Hi, I'm trying to insert 2 records in a bbdd and I really get an error that I can not understand since I'm a bit of a beginner in this, I would appreciate if someone could give me a cable indicating if something in my code could be wrong .
public class JSONparser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
public JSONparser() {
}
public JSONObject makeHttpRequest(String url, String method,
List<NameValuePair> params) {
try {
if(method == "POST"){
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}else if(method == "GET"){
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
}
catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
class CrearAviso extends AsyncTask<String,String,String> {
String url_crear_control = "http://192.168.2.19/android/control/insert.php";
@Override
protected void onPreExecute() {
super.onPreExecute();
dialogo = new ProgressDialog(Main2Activity.this);
dialogo.setMessage("Creating Product..");
dialogo.setIndeterminate(false);
dialogo.setCancelable(true);
dialogo.show();
}
@Override
protected String doInBackground(String... strings) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
String position=posicion.getText().toString();
String acidente="accidente";
String control="control";
String radar="radar";
if (radio.getCheckedRadioButtonId()== R.id.radioButton_accidente){
params.add(new BasicNameValuePair("imagen",acidente));
}else if(radio.getCheckedRadioButtonId()== R.id.radioButton_control){
params.add(new BasicNameValuePair("imagen",control));
}else if(radio.getCheckedRadioButtonId()== R.id.radioButton_radar){
params.add(new BasicNameValuePair("imagen",radar));
}
params.add(new BasicNameValuePair("posicion", position));
JSONObject json = jsonParser.makeHttpRequest(url_crear_control,
"POST", params);
Log.d("Create Response", json.toString());
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully created product
Intent i = new Intent(getApplicationContext(), MainActivity.class);
startActivity(i);
// closing this screen
finish();
} else {
// failed to create product
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
}
}
Error:
Value null of type org.json.JSONObject $ 1 can not be converted to JSONObject