gpt4 book ai didi

android - 如何在asynctask中写入解析后的json

转载 作者:搜寻专家 更新时间:2023-11-01 07:54:58 25 4
gpt4 key购买 nike

我正在开发通过添加参数用户名和密码从 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);
}

最佳答案

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

关于android - 如何在asynctask中写入解析后的json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29246310/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com