android - 无法将大字符串上传到服务器

标签 android string

我正在尝试通过邮寄将字符串上传到服务器..这是我的代码..

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();
}

当我上传一个小字符串时,此代码有效。但是当我尝试发送大字符串(超过 2 行)时,例如,它不起作用。 有什么建议吗?

谢谢你:)

最佳答案

Method Get has Maximium Chars limit , 而不是使用发布

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();
}

关于android - 无法将大字符串上传到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24488395/

相关文章:

android - ViewPager InstatiateItem 方法被多次调用

android - IntentReceiver 不起作用

android - Activity 退出时的 RecyclerView 动画

android - 在下载文件夹中打开文件 Android N(API 24+)

string - 提取字符串的前(或后)n 个字符

ruby - 选择一个字符串,直到 100 个字符后的第一个句点

带有 scanf : nonrestricted char array doesn't give correct result 的字符数组

Android 更改 ToggleButton 状态 - TextView 或其他 android.widget

python - 为什么python双引号在文件名中转换为连字符?

C++ std::string 在任何平台上都是 100% RAII?