【发布时间】: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