带有参数的android http帖子

时间:2010-07-06 04:23:08

标签: android http http-post

我想从客户端(Android)向服务器发送数据,下面是我的格式

http://101.34.45.45/rawData?data={"userId":"guest1","timestamp":"2010-07-01 08:58:23","wifi":[{"ssid":"guest","rssi":"40"},{"ssid":"guest1","rssi":"80"}]}, 

我正在尝试一系列试用但没有用,怎么做呢?

2 个答案:

答案 0 :(得分:0)

如果没有您的代码,很难评估您是否正确地完成了这个过程,但根据您提供给我们的内容,您没有正确编码数据。在附加到URL之前,您需要对数据进行URL编码。要在java中执行此操作,请使用URLEncoder(see the javadoc for that class here)

要对您的代码执行此操作,它看起来像

String url = "http://101.34.45.45/rawData?data=";
String params = URLEncoder.encode("{\"userId\":\"guest1\",\"timestamp\":\"2010-07-01 08:58:23\",\"wifi\":[{\"ssid\":\"guest\",\"rssi\":\"40\"},{\"ssid\":\"guest1\",\"rssi\":\"80\"}]}",  "UTF-8");
url = url+params;
//do the HTTP operation

*注意,为了完整起见,W3C和javadoc主张不使用UTF-8。我在这里使用它只是为了生成一些示例代码。

如果这不能解决您的问题,请发布您的代码,以便我们了解您的目标。

答案 1 :(得分:0)

使用的图书馆:http://loopj.com/android-async-http/

private OnClickListener login = new OnClickListener() {

public void onClick(View view) {

    AsyncHttpClient myClient = new AsyncHttpClient();
    myClient.get(URL, null);
    myClient.setCookieStore(myCookieStore);
    myClient.setCookieStore(myCookieStore);
    String username = "";
    String password = "";
    RequestParams params1 = new RequestParams();
    params1.put("username", username);
    params1.put("password", password);
    pd = ProgressDialog.show(this, "", "Signing In...");
    myClient.post(URL, params1,
            new AsyncHttpResponseHandler() {
                @Override
                public void onSuccess(String response) {
                    System.out.println("response" + response);
                    pd.dismiss();
                    if (response.contains("<!--Authorized-->")) {
                    }
                    else {
                        pd.dismiss();
                        Context mContext = SigninActivity.this;
                        notMatchDialog = new Dialog(mContext);
                        notMatchDialog.setContentView(R.layout.loginfaileddialoglayout);
                        notMatchDialog.setTitle("Login failed");
                        dismissDialogButton = (Button) notMatchDialog.findViewById(R.id.dismissDialogButton);
                        dismissDialogButton.setOnClickListener(dismissDialog);
                        notMatchDialog.show();
                    }
                }

                @Override
                public void onFailure(Throwable e, String response) {
                    // TODO Need to figure out different failures and try to help the user.
                }
            });
}
};