0

我有点沮丧。我有问题。我正在尝试通过 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是否正确,对吗?

我希望有人能在这种情况下帮助我!

提前致谢。

4

1 回答 1

0

好吧,这太复杂了,无法自己处理,看看这个库,它有更干净的 API 和类型安全的数据。

https://github.com/kodart/Httpzoid

这是一个简单的使用示例

Http http = HttpFactory.create(context);
http.post("http://example.com/users")
    .data(new User("John"))
    .execute();

或者更复杂的回调

Http http = HttpFactory.create(context);
http.post("http://example.com/users")
    .data(new User("John"))
    .handler(new ResponseHandler<Void>() {
        @Override
        public void success(Void ignore, HttpResponse response) {
        }

        @Override
        public void error(String message, HttpResponse response) {
        }

        @Override
        public void failure(NetworkError error) {
        }

        @Override
        public void complete() {
        }
    }).execute();

它是新的,但看起来很有前途。

于 2013-07-14T05:50:22.463 回答