Error parsing data org.json.JSONException: Value !DOCTYPE of type java.lang.String cannot be converted to JSONObject
-
I'm trying to get the data on the server, but it's gonna make this mistake in the loga. After that, she's made a mistake in the blade of Fatal Error: AsynkTask#1. That's the log:
`E/JSON Parser: Error parsing data org.json.JSONException: Value <!doctype of type java.lang.String cannot be converted to JSONObject 07-21 14:49:00.343 15140-15409/avdeewkiril.paybillet D/dalvikvm: create interp thread : stack size=32KB 07-21 14:49:00.343 15140-15409/avdeewkiril.paybillet D/dalvikvm: create new thread 07-21 14:49:00.343 15140-15409/avdeewkiril.paybillet D/dalvikvm: new thread created 07-21 14:49:00.343 15140-15409/avdeewkiril.paybillet D/dalvikvm: update thread list 07-21 14:49:00.343 15140-15417/avdeewkiril.paybillet D/dalvikvm: threadid=14: interp stack at 0x5ed9c000 07-21 14:49:00.343 15140-15417/avdeewkiril.paybillet D/dalvikvm: threadid=14: created from interp 07-21 14:49:00.343 15140-15409/avdeewkiril.paybillet D/dalvikvm: start new thread 07-21 14:49:00.343 15140-15409/avdeewkiril.paybillet D/dalvikvm: threadid=13: exiting 07-21 14:49:00.343 15140-15409/avdeewkiril.paybillet W/dalvikvm: threadid=13: thread exiting with uncaught exception (group=0x4193a9a8) 07-21 14:49:00.344 15140-15417/avdeewkiril.paybillet D/dalvikvm: threadid=14: notify debugger 07-21 14:49:00.344 15140-15417/avdeewkiril.paybillet D/dalvikvm: threadid=14 (AsyncTask #2): calling run() 07-21 14:49:00.345 15140-15409/avdeewkiril.paybillet E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1 java.lang.RuntimeException: An error occured while executing doInBackground() at android.os.AsyncTask$3.done(AsyncTask.java:299) at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352) at java.util.concurrent.FutureTask.setException(FutureTask.java:219) at java.util.concurrent.FutureTask.run(FutureTask.java:239) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) at java.lang.Thread.run(Thread.java:838) Caused by: java.lang.NullPointerException at avdeewkiril.paybillet.MainActivity$NewBillet.doInBackground(MainActivity.java:242) at avdeewkiril.paybillet.MainActivity$NewBillet.doInBackground(MainActivity.java:219) at android.os.AsyncTask$2.call(AsyncTask.java:287) at java.util.concurrent.FutureTask.run(FutureTask.java:234) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) at java.lang.Thread.run(Thread.java:838) `
This is the code.
class NewBillet extends AsyncTask<String, String, String> {
@Override protected void onPreExecute(){ super.onPreExecute(); progressDialog = new ProgressDialog(MainActivity.this); progressDialog.setMessage("Бронирование биллета"); progressDialog.setIndeterminate(false); progressDialog.setCancelable(true); progressDialog.show(); } protected String doInBackground(String[]args){ String pay_status = "no"; String real_date_from = args[0]; String real_date_back = args[1]; List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("real_date_from", real_date_from)); params.add(new BasicNameValuePair("real_date_back", real_date_back)); params.add(new BasicNameValuePair("pay_status", pay_status)); JSONObject json = jsonParser.makeHttpRequest(url_create_new_ticket, "POST", params); Log.d("Create Response", json.toString()); try{ int success = json.getInt(TAG_SUCCES); if (success == 1){ startActivity(new Intent(getApplicationContext(), Tickets.class)); finish(); } } catch (JSONException e){ e.printStackTrace(); } return null; } protected void onPostExecute(String file_url){ progressDialog.dismiss(); } } public void Bronirovat(View v) { TextView dateFrom =(TextView)findViewById(R.id.dateStart); TextView dateBack = (TextView)findViewById(R.id.dateEnd); new NewBillet().execute(dateFrom.getText().toString(), dateBack.getText().toString()); }
Log refers to the line.
Log.d("Create Response", json.toString());
As far as I understand, the mistake is that JSON can't convert the line into the right type.
This is JSONParser class:public class JSONParser {
static InputStream is = null; static JSONObject jObj = null; static String json = ""; // constructor public JSONParser() { } // function get json from url
// by making HTTP POST or GET method
public JSONObject makeHttpRequest(String url, String method,
List params) {// Making HTTP request
try {// check for request method
if(method == "POST"){
// request method is POST
// defaultHttpClient
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"){
// request method is 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 parse the string to a JSON object
try {
jObj = new JSONObject (json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}// return JSON String
return jObj;}
}
I'm also attaching php, because I think I might've done something there:
<?php
$response = array();if (isset($_POST['real_date_from']) && isset($_POST['real_date_back']) && isset($_POST['pay_status'])) {
$real_date_from = $_POST['real_date_from']; $real_date_back = $_POST['real_date_back']; $pay_status = $_POST['pay_status']; require 'db_connect.php'; $db = new DB_CONNECT(); $result = mysql_query("INSERT INTO bus_order(real_date_from, real_date_back, pay_status) VALUES('$real_date_from', '$real_date_back', '$pay_status')"); if ($result) { $response["success"] = 1; $response["message"] = "Product successfully created."; echo json_encode($response); } else { $response["success"] = 0; $response["message"] = "Oops! An error occurred."; echo json_encode($response); }
} else {
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";echo json_encode($response);
}
?>
-
The mistake speaks for itself, Yours.
JSONObject json
Not JSON at all. ♪- See if your url looks right.
- If it's right, look what type of data he's returning.
- That's how the lines don't compare.
if(method == "POST")
♪ Lines, like any other objects, shall be compared throughif(stringA.equals(stringB))
- Underneath the debago, look where it falls into you.
makeHttpRequest