2014-06-10 65 views
-1

你好,我怎麼可以解析從URL JSON類型這樣JSON數據{「代碼」:200,「郎」:「恩」}我怎麼能解析JSON數據android開發

請幫助我瞭解這個。 我使用類jsonparser

JSONParser.class

public class JSONParser { 

    Context Context; 
    static InputStream is = null; 
    static JSONObject jobj = null; 
    static String json = ""; 

    public JSONParser() { 

    } 

    public JSONObject makeHttpRequest(String url) { 
     DefaultHttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost(url); 
     try { 
      HttpResponse httpresponse = httpclient.execute(httppost); 
      int code = httpresponse.getStatusLine().getStatusCode(); 
      if (code == 200) { 
       HttpEntity httpentity = httpresponse.getEntity(); 
       is = httpentity.getContent(); 

      } else { 

       Toast.makeText(Context, "error ", Toast.LENGTH_LONG).show(); 
      } 

     } catch (ClientProtocolException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     try { 
      BufferedReader reader = new BufferedReader(new InputStreamReader(
        is, "utf-8"), 8); 
      StringBuilder sb = new StringBuilder(); 
      String line = null; 
      try { 
       while ((line = reader.readLine()) != null) { 
        sb.append(line + "\n"); 

       } 
       is.close(); 
       json = sb.toString(); 
       try { 
        jobj = new JSONObject(json); 
       } catch (JSONException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } catch (IOException e) { 

       e.printStackTrace(); 
      } 

     } catch (UnsupportedEncodingException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     return jobj; 

    } 

} 

MainActivity.class

-package,imports- 

public class MainActivity extends Activity { 
    TextView uid; 
    String ab; 
    TextView name1; 
    TextView email1; 
    Button Btngetdata; 
    // URL to get JSON Array 
    JSONParser jsonparser = new JSONParser(); 
    private static String url = "https://translate.yandex.net/api/v1.5/tr.json/detect?key=API KEY&text=hi";// JSON 

//從URL結果{ 「代碼」:200, 「郎」: 「恩」}

                                             // Node 
                                                 // Names 
    private static final String TAG_USER = "lang"; 

    JSONArray user = null; 
    JSONObject jobj = null; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     new JSONParse().execute(); 
    } 

    class JSONParse extends AsyncTask<String, String, String> { 
     private ProgressDialog pDialog; 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      uid = (TextView) findViewById(R.id.textView1); 

      pDialog = new ProgressDialog(MainActivity.this); 
      pDialog.setMessage("Getting Data ..."); 
      pDialog.setIndeterminate(false); 
      pDialog.setCancelable(true); 
      pDialog.show(); 
     } 

     @Override 
     protected String doInBackground(String... arg0) { 

      // TODO Auto-generated method stub 


      jobj = jsonparser.makeHttpRequest(url); 

      // check your log for json response 
      Log.d("Login attempt", jobj.toString()); 

      JSONArray ar = jobj.optJSONArray("lang"); 
      if (ar != null) { 
       return ar.optString(0); 
      } 
      return ab; 
     } 


     @Override 
     protected void onPostExecute(String ab) { 
      pDialog.dismiss(); 

      } 
     } 
} 
+1

你爲什麼不告訴我們到底是什麼問題您有 – codeMagic

+0

發表您的logcat的...... –

回答

0

希望這對你有效。你所犯的錯誤是你得到了JSONObject到JSONArray。

下面是修改JSONParser異步任務代碼JSONParse

感謝