【问题标题】:How to Parse API response in android?如何在 android 中解析 A​​PI 响应?
【发布时间】:2019-09-28 10:46:00
【问题描述】:

我们如何在android中解析响应?

API 响应

{"id":29,"name":"demo","email":"demo@gmail.com"}

如何获取 id、name 和 email 的值。

AsyncHttpClient client = new AsyncHttpClient();
client.get("http://XXX.XX.X.XXX/api/api.php?apicall=login", params, new TextHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, String response) {
 prgDialog.hide();
 Toast.makeText(getApplicationContext(), response, Toast.LENGTH_LONG).show();
 try {
   JSONObject jObj = new JSONObject(response);
   Toast.makeText(getApplicationContext(), jObj.getString("email"), Toast.LENGTH_LONG).show();
 } catch (JSONException e) {
   e.printStackTrace();
   Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
 }
}
});

【问题讨论】:

标签: android parsing


【解决方案1】:

像这样替换你的代码,如果你有多个对象,这就是你需要解析的方式,那么你必须使用循环。

 AsyncHttpClient client = new AsyncHttpClient();
    client.get("http://XXX.XX.X.XXX/api/api.php?apicall=login", params, new TextHttpResponseHandler() {
        @Override
        public void onSuccess(int statusCode, Header[] headers, String response) {
            prgDialog.hide();
            Toast.makeText(getApplicationContext(), response, Toast.LENGTH_LONG).show();
            try {
                JSONObject reader = new JSONObject(response);
                int id  = reader.getInt("id");
                String name  = reader.getString("name");
                String email  = reader.getString("email");
            } catch (JSONException e) {
                e.printStackTrace();
                Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
            }
        }
    });

【讨论】:

    【解决方案2】:

    试试这个:

         Thread thread = new Thread(new Runnable() {
    
                @Override
                public void run() {
                    try {
    
                        String mainUrl = "http://YOUR_ADRESS";
    
    
                        StringBuilder sbPostData = new StringBuilder(mainUrl);
                        mainUrl = sbPostData.toString();
                        try {
                            //prepare connection
                            URL myURL = new URL(mainUrl);
                            URLConnection myURLConnection = myURL.openConnection();
                            myURLConnection.connect();
                            BufferedReader reader = new BufferedReader(new InputStreamReader(myURLConnection.getInputStream()));
    
                            //reading response
                            String response;
                            response = reader.readLine();
                            JSONArray jsonarray = new JSONArray(response);
    
                            JSONObject jsonobject0 = jsonarray.getJSONObject(0);
                                JSONObject jsonobject0 = jsonarray.getJSONObject(0);
    
    //getting the email and save it in EMAIL string
    //you can do the other parts like this. its easy 
    
                            String EMAIL =  jsonobject0.getString("email");
    
    
                            while ((response = reader.readLine()) != null)
                                //print response
                                Log.d("RESPONSE", ""+response);
                            //finally close connection
                            reader.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
    
                    } catch (Exception e) {
                        e.printStackTrace();
    
                    }
                }
            });
    
            thread.start();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-26
      • 1970-01-01
      • 2013-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多