我有点沮丧。我有问题。我正在尝试通过 put REST 请求将用户的所有输入发送到服务器。这是请求 json 对象在服务器上的外观:
{
id: "123",
text: "My Name is Peter...,
age": 15,
name: "Peter",
hobbies: [
id": 321,
hobbie_id": 1,
name": "Football",
},
id": 213,
hobbie_id": 1,
name": "Basketball",
}
],
gender: true,
version: 1
}
我有一个内部类的类,它从 AsyncTask 扩展来处理后台服务器通信:
public class MyActivity extends Activity{
private class PutServiceTask extends AsyncTask<String, Integer, String> {
@Override
protected void onPreExecute() {
//show Dialogbox
}
@Override
protected String doInBackground(String... urls) {
String url = urls[0];
String result = "";
HttpResponse response = doResponse(url);
if (response == null) {
return result;
}
return result;
private HttpResponse doResponse(String url) {
HttpClient httpclient = new DefaultHttpClient();
HttpPut httpput = new HttpPut(url);
HttpResponse response = null;
// Add parameters
try {
response = httpclient.execute(httpput);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if(statusCode == HttpStatus.SC_OK){ //200
httpput.addHeader("content-type", "application/json");
StringEntity se = new StringEntity(params.toString());
httpput.setEntity(se);
HttpEntity entity = response.getEntity();
}
else {
System.out.println(response.getStatusLine().getReasonPhrase());
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return response;
}
}
}
}
现在的问题是如何在 onPostExecute Methode 中将其放在服务器上。这些是我从即 EditText 获得并转换为字符串的用户输入:
private String ageString; //age
private String nameString; //name of the Person
private String hobbyname; //name of the Hobby
private String textString; //text
另一件事是身份证。服务器生成ID是否正确,对吗?
我希望有人能在这种情况下帮助我!
提前致谢。