1

I am trying to upload a string to server by post.. This is my code..

String URL = "http://myUrl/add?user_id="+user_id
        + "&text=" + text 
        + "&type=" + type;

URL = URL.replaceAll(" ", "%20");
URL = URL.replaceAll("\n", "%0A");
try {
    // defaultHttpClient
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(URL);

    HttpResponse httpResponse = httpClient.execute(httpPost);
    HttpEntity httpEntity = httpResponse.getEntity();
    String result = EntityUtils.toString(httpEntity);

} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
} catch (ClientProtocolException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

This code is working when i am uploading a small string.. but when i am trying to send large string (more than 2 lines) for example.. it doesn't work.. Any suggestions?

Thank you :)

Arwa
  • 161
  • 9

4 Answers4

1

Method Get has Maximium Chars limit, instead Use Post

String URL = "http://myUrl/add";
try {
    // defaultHttpClient
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(URL);
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("user_id", user_id));
    nameValuePairs.add(new BasicNameValuePair("text", text));
    nameValuePairs.add(new BasicNameValuePair("type", type));
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    HttpResponse httpResponse = httpClient.execute(httpPost);
    HttpEntity httpEntity = httpResponse.getEntity();
    String result = EntityUtils.toString(httpEntity);

    } catch (UnsupportedEncodingException e) {
    e.printStackTrace();
    } catch (ClientProtocolException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
}
Community
  • 1
  • 1
VenomVendor
  • 15,064
  • 13
  • 65
  • 96
0

You should use \" to send a string:

String URL = "myUrl/add?user_id="+user_id 
+ "\"&text=\"" + text 
+ "\"&type=\"" + type; 

Also you should use POST instead GET

0

You transfer your data as part of the query string, which is part of the URL. There is a maximum length on URLs which depends heavily on the client and server software. As far as I know there is no standard which defines this maximum.

Anyways, you should transfer a large amount of data in the HTTP request body. See here for an example.

Oliver Hanappi
  • 12,046
  • 7
  • 51
  • 68
0

Use NameValuePair for sending the data, you are using HTTP POST to send the data in GET Style. I will strongly recommend use of NameValuePair to send the data.A small example is List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("email","email")); DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url); httpPost.setEntity(new UrlEncodedFormEntity(params)); HttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity httpEntity = httpResponse.getEntity();

Kshitij
  • 144
  • 10