Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You are expecting a JSONArray back, but the webservice is just returning a JSONObject.</p> <p>I made the following changes:</p> <p><code>JSONArray getname = new JSONArray(data);</code> --> <code>JSONObject getname = new JSONObject(data);</code></p> <p>and in the Async Task, I made the following change:</p> <p><code>return json.getString("firstName");</code></p> <p>Here's the complete code:</p> <pre><code>public class PlaygroundActivity extends Activity { TextView http; HttpClient client; JSONObject json; final static String URL = "REMOVED FOR PRIVACY CONCERNS @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); http = (TextView) findViewById(R.id.http); client = new DefaultHttpClient(); new Read().execute("firstName"); } public JSONObject getpw(String password) throws ClientProtocolException, IOException, JSONException { StringBuilder url = new StringBuilder(URL); url.append(password); 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); JSONObject getname = new JSONObject(data); return getname; } else { Toast.makeText(PlaygroundActivity.this, "error", Toast.LENGTH_SHORT); return null; } } public class Read extends AsyncTask&lt;String, Integer, String&gt; { @Override protected String doInBackground(String... arg0) { // TODO Auto-generated method stub try { json = getpw("trustme"); return json.getString("firstName"); } 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 http.setText(result); } } } </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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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