Note that there are some explanatory texts on larger screens.

plurals
  1. PONot able to get Json from php in android?
    primarykey
    data
    text
    <p>I am getting this Logcat: </p> <pre><code>08-14 11:42:55.923: W/System.err(1137): org.json.JSONException: Value {"error":0,"success":0,"tag":"helloworld"} of type org.json.JSONObject cannot be converted to JSONArray </code></pre> <p>How do I resolve this problem? </p> <pre><code>08-14 11:42:55.923: W/System.err(1137): at org.json.JSON.typeMismatch(JSON.java:111) </code></pre> <p>The Code I am using is: </p> <pre><code>public class MainActivity extends Activity { TextView tv; HttpClient client; JSONObject json; final static String URL = "http://192.168.1.9/sumit/hello.php"; //final static String URL = "http://json.org/example.html"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv = (TextView) findViewById(R.id.tv1); client = new DefaultHttpClient(); Log.e("my","check"); new Read().execute("tag"); // StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder() // .detectAll().penaltyLog().build(); // StrictMode.setThreadPolicy(policy); } public JSONObject myData() throws ClientProtocolException, IOException, JSONException { StringBuilder url = new StringBuilder(URL); HttpGet get = new HttpGet(url.toString()); HttpResponse r = client.execute(get); int status = r.getStatusLine().getStatusCode(); if (status == 200) { HttpEntity e = r.getEntity(); String data = EntityUtils.toString(e); JSONArray datastream = new JSONArray(data); JSONObject message = datastream.getJSONObject(0); return message; } else { Toast.makeText(MainActivity.this, "error encountered", Toast.LENGTH_SHORT).show(); return null; } } class Read extends AsyncTask&lt;String, Integer, String&gt; { @Override protected String doInBackground(String... params) { // TODO Auto-generated method stub try { json = myData(); return json.getString(params[0]); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } @Override protected void onPostExecute(String result) { // TODO Auto-generated method stub tv.setText(result); } } } </code></pre> <p>JSON code generated: </p> <pre><code>{"tag":"hello","success":0,"error":0} </code></pre> <p>Code used to generate:</p> <pre><code>&lt;?php // response Array $response = array("tag" =&gt; helloworld, "success" =&gt; 0, "error" =&gt; 0); echo json_encode($response); ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload