Android 如何在asynctask中编写解析的json

Android 如何在asynctask中编写解析的json,android,json,android-asynctask,Android,Json,Android Asynctask,我正在开发一个应用程序,通过添加参数username和pwd解析URL中的数据,并对其进行更新,解析已完成,但我无法更新或写/发回服务器 private class abc extends AsyncTask<String,String,String>{ String response = ""; @Override protected String doInBackground(String... arg0) { // TODO Auto

我正在开发一个应用程序,通过添加参数username和pwd解析URL中的数据,并对其进行更新,解析已完成,但我无法更新或写/发回服务器

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);
}   
私有类abc扩展异步任务{
字符串响应=”;
@凌驾
受保护的字符串doInBackground(字符串…arg0){
//TODO自动生成的方法存根
DefaultHttpClient=新的DefaultHttpClient();
HttpPost p1=新的HttpPost(“Url”);
Listdata=新的ArrayList(2);
添加(新的BasicNameValuePair(“用户名”、“abc”));
添加(新的BasicNameValuePair(“密码”、“定义”);
试一试{
p1.setEntity(新的UrlEncodedFormEntity(数据));
HttpResponse execute=client.execute(p1);
InputStream内容=execute.getEntity().getContent();
BufferedReader buffer=新的BufferedReader(新的InputStreamReader(内容));
字符串s=“”;
而((s=buffer.readLine())!=null){
响应+=s;
}             
}捕获(不支持的编码异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(客户端协议例外e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}  
返回响应;
}
@凌驾
受保护的void onPostExecute(字符串结果){
//TODO自动生成的方法存根
试一试{
JSONObject inf=新的JSONObject(结果);
int id=inf.getInt(“UserID”);
字符串username=inf.getString(“用户名”);
TextView t1=(TextView)findViewById(R.id.textView1);
t1.setText(“用户id:+id+”\n“+”用户名:+UserName);
}捕获(JSONException e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}       
super.onPostExecute(结果);
}   

我不完全明白你的问题

如果要根据接收到的数据更新服务器上的数据,可以在正确发送响应之前进行更新。 为什么要发出另一个请求并更新数据

在服务器上更新数据将使用服务器端逻辑,而不是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
}
私有类abcupdate扩展异步任务{
字符串响应=”;
@凌驾
受保护的字符串doInBackground(字符串…arg0){
//TODO自动生成的方法存根
DefaultHttpClient=新的DefaultHttpClient();
HttpPost p1=新的HttpPost(“Urlforupdate”);
Listdata=新的ArrayList(4);
添加(新的BasicNameValuePair(“用户名”、“abc”));
添加(新的BasicNameValuePair(“密码”、“定义”);
添加(新的BasicNameValuePair(“userId”、“userIdyouwant”);
数据。添加(新的BasicNameValuePair(“数据PT1”、“PQR”);
试一试{
p1.setEntity(新的UrlEncodedFormEntity(数据));
HttpResponse execute=client.execute(p1);
InputStream内容=execute.getEntity().getContent();
BufferedReader buffer=新的BufferedReader(新的InputStreamReader(内容));
字符串s=“”;
而((s=buffer.readLine())!=null){
响应+=s;
}             
}捕获(不支持的编码异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(客户端协议例外e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}  
返回响应;
}
@凌驾
受保护的void onPostExecute(字符串结果){
//TODO自动生成的方法存根
super.onPostExecute(结果);
//用回应做你需要的事
//可能只是从服务器发送更新的成功或失败
}

希望这已经清除了所有内容

在doInBackground中使用用户名、密码和用户ID传递您的参数
用户名和密码是您当前的用户名和密码(如get-edittext值)然后用post方法调用asyntask

问题是什么?我很困惑我的问题就像从url解析用户信息并更新它,我已经完成了解析,但下一步该怎么办?要返回服务器,我需要更新用户名和密码我如何做plz帮助仍然不清楚您的问题是什么。从上面的代码中,您正在传递用户名和密码assword并向“url”发送帖子。您将得到Json响应。如果您想再次更新它,请使用新值再次调用异步任务。不确定您到底需要什么。实际上,首先我希望从url解析Json数据,如用户名id pwd等,解析后我需要更新/更改这些用户名id pwd,好的,那么我如何将数据发布到服务器,因为在setentit上y我已经在发布用户名和pwdy当您放置键值对时,您正在向服务器发布数据。您只需要在服务器端访问此数据。在放置用户名和pwd后,我正在获取所有数据,现在我想更改用户名和剩余内容,而不更改用户名和pwd。我可以使用outputstream writer或jsonwri吗ter?那么,在您得到响应并检索到用户ID和其他内容后,您需要更新服务器数据??我是否正确?您是否可以只在服务器上发布您需要的总流量,除了两个参数username和pwd,他们使用这两个参数发送该用户的数据,现在我需要更新该数据,我怎么做?不,这一个不工作我已经尝试过了,通过添加更多参数它仍然只显示数据它不更新