【问题标题】:How to write the parsed json in asynctask如何在 asynctask 中编写解析的 json
【发布时间】:2015-03-25 02:12:37
【问题描述】:

我正在开发应用程序,该应用程序通过添加参数用户名和密码来解析来自 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);
}   

【问题讨论】:

  • 问题是什么? 我很困惑
  • 我的问题是从 url 解析用户信息并更新它,我已经完成了解析但是接下来呢?
  • 返回服务器,我需要更新用户名和密码,请问如何帮助
  • 仍不清楚您的要求。从上面的代码中,您将传递用户名和密码并对“url”进行 POST。你得到 Json 响应。如果您想再次更新它,请使用新值再次调用异步任务。不太确定您到底需要什么。
  • 其实首先我想从 url 解析 json 数据,比如用户名 id pwd 等,解析后我需要更新/更改那些用户名 id pwd,

标签: android json android-asynctask


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    这里是一个更新的例子

    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
    }
    

    希望这已经清除了一切

    【讨论】:

    • 不,这个不工作我已经试过了,通过添加更多参数它仍然显示数据只是它没有更新
    • 如果您发送到服务器的数据显示在那里,那么上面的部分是正确的,因为它只是为了发送数据。在服务器上获取数据后,您需要编写一个查询来更新您的服务器数据库。只有这样,更新才会发生。你这样做吗?
    • 例如我使用这个 url "114.143.190.99:81/Feeds/api/Authentication/Authenticate" 和 ("UserName", "paresh"));("Password", "paresh")); , 我正在获取他们的数据 id 以及更多现在我需要更改 id 我该怎么办??
    • 请执行以下操作:
    • "114.143.190.99:81/Feeds/api/Update" 可能是您定义的用于接受更新数据请求的新路由。现在在这条路线的逻辑中,进行更新查询!
    【解决方案3】:

    我没有完全理解你的问题。

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

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

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

    【讨论】:

    • 好的,那么我如何将数据发布到服务器,因为在 setentity 上我已经发布了用户名和密码
    • 当您放置键值对时,您正在将数据发布到服务器。你只需要在服务器端访问这些数据
    • 在输入用户名和密码后,我正在获取所有数据,现在我想更改用户名和剩余内容,而不更改用户名和密码。我可以使用 outputstream writer 或 jsonwriter 吗?
    • 那么,在你得到响应并检索到用户ID和其他东西之后,你需要更新服务器数据吗?我做对了吗?你能在这里发布你需要的总流量吗
    • 服务器只有两个参数 username 和 pwd 用这两个参数发送该用户的数据,现在我需要更新该数据,我该怎么办??
    猜你喜欢
    • 2015-05-02
    • 2018-10-22
    • 2020-12-31
    • 2013-06-08
    • 2017-05-13
    • 1970-01-01
    • 1970-01-01
    • 2015-05-22
    相关资源
    最近更新 更多