如何在 asynctask 中写入已解析的 json

How to write the parsed json in asynctask

我正在开发应用程序,该应用程序通过添加参数用户名和密码来解析来自 URL 的数据并更新它,解析已完成但我无法更新或 write/post 返回服务器。

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);
}   

我不完全明白你的问题。

如果你想根据收到的数据更新服务器上的数据,你可以在发送响应之前完成。 为什么要再次请求并更新数据。

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

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

这里是一个更新的例子

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
}

希望这已经解决了所有问题

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

调用 asyntask