如何在asynctask中编写解析的json

时间:2015-03-25 02:12:38

标签: android json android-asynctask

我正在使用app解析来自URL的数据,添加参数username和pwd并更新它,解析完成但我无法更新或写回/发回服务器。

private class abc extends AsyncTask<String,String,String>{

    String response = "";

    @Override
    protected String doInBackground(String... arg0) {
        // TODO Auto-generated method stub

        DefaultHttpClient client = new DefaultHttpClient();
        HttpPost p1 = new HttpPost("Url");

        List<NameValuePair>data = new ArrayList<NameValuePair>(2);               
            data.add(new BasicNameValuePair("UserName", "abc"));
            data.add(new BasicNameValuePair("Password", "def"));

            try {
                  p1.setEntity(new UrlEncodedFormEntity(data));
                  HttpResponse execute = client.execute(p1);
                  InputStream content = execute.getEntity().getContent();
                  BufferedReader buffer = new BufferedReader(new InputStreamReader(content));
                  String s = "";
                  while ((s = buffer.readLine()) != null) {
                    response += s;
                  }             

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

@Override
protected void onPostExecute(String result) {
    // TODO Auto-generated method stub

    try {
        JSONObject inf = new JSONObject(result);
        int id = inf.getInt("UserID");
        String username = inf.getString("UserName");

        TextView t1 = (TextView)findViewById(R.id.textView1);
        t1.setText("user id :"+id+"\n"+"user name :"+UserName);

    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }       
    super.onPostExecute(result);
}   

3 个答案:

答案 0 :(得分:3)

使用用户名和密码以及userId在doInBackground中传递参数 用户名和密码是您当前的用户名和密码(如获取edittext值)并使用post方法调用asyntask

答案 1 :(得分:1)

以下是更新的示例

private class abcupdate extends AsyncTask<String,String,String>{
String response = "";

@Override
protected String doInBackground(String... arg0) {
    // TODO Auto-generated method stub

    DefaultHttpClient client = new DefaultHttpClient();
    HttpPost p1 = new HttpPost("Urlforupdate");

    List<NameValuePair>data = new ArrayList<NameValuePair>(4);               
        data.add(new BasicNameValuePair("UserName", "abc"));
        data.add(new BasicNameValuePair("Password", "def"));
        data.add(new BasicNameValuePair("userId", "userIdyouwant"));
        data.add(new BasicNameValuePair("datapt1", "pqrs"));


        try {
              p1.setEntity(new UrlEncodedFormEntity(data));
              HttpResponse execute = client.execute(p1);
              InputStream content = execute.getEntity().getContent();
              BufferedReader buffer = new BufferedReader(new InputStreamReader(content));
              String s = "";
              while ((s = buffer.readLine()) != null) {
                response += s;
              }             

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

@Override
protected void onPostExecute(String result) {
    // TODO Auto-generated method stub    
super.onPostExecute(result);
//do what you need wwith response
//maybe just send success or failure of update from server
}

希望这已经清除了一切

答案 2 :(得分:0)

我完全不接受你的问题。

如果要根据收到的数据更新服务器上的数据,可以在向右发送响应之前执行此操作。 为什么要发出另一个请求并更新数据。

更新服务器上的数据将是您的服务器端逻辑而不是Android代码。

如果这不是你想要的,请更清楚地解释一下这个问题; - )