开发者

android http post with parameter

开发者 https://www.devze.com 2023-01-06 00:28 出处:网络
I want to send data to server from client(Android), below is my format http://101.34.45.45/rawData?data={\"userId\":\"guest1\",\"timestamp\":\"2010-07-01 08:58:23\",\"wifi\":[{\"ssid\":\"guest\",\"r

I want to send data to server from client(Android), below is my format

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"}]},开发者_JAVA技巧 

i'm trying series of trial but no use, how this can be done?


Without your code it is difficult to assess whether you are going through the process correctly, but based on what you've given us, you aren't properly encoding the data. You will need to URL encode the data before appending to the url. To do this in java, use the URLEncoder class (see the javadoc for that class here).

To do this with your code, it would look something like

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

*Note, for completeness, the W3C and the javadoc advocate not using UTF-8. I used it here simply to generate some sample code.

If that doesn't solve your problem, please post your code so we can see what you're trying to do.


Library used: 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.
                }
            });
}
};
0

精彩评论

暂无评论...
验证码 换一张
取 消