HttpPost不接受Java中冗长的url

HttpPost不接受Java中冗长的url,java,android,json,http,http-post,Java,Android,Json,Http,Http Post,这是我的android应用程序中的httppost方法。它不接受宽松的URL。对于冗长的URL,没有响应/例外。当我在浏览器中手动输入相同的url时,效果很好。有人能指出这个问题吗 try { // defaultHttpClient DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url);

这是我的android应用程序中的httppost方法。它不接受宽松的URL。对于冗长的URL,没有响应/例外。当我在浏览器中手动输入相同的url时,效果很好。有人能指出这个问题吗

    try {
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();           

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
更新: 添加了一个示例url。当在浏览器中手动输入相同的url并给出响应时,该url可以正常工作

 url.com/data?format=json&pro={%22merchanturl%22:%22http://url.com/logo.pn‌​g%22,%22price%22:599,%22productDesc%22:%22Apple%2032GBBlack%22,%22prodID%22:%2291‌​3393%22,%22merchant%22:%224536%22,%22prourl%22:%22http://url.com/data%22,%22name%‌​22:%22Apple%2032GB%20%2D%20Black%22,%22productUrl%22:%22http://www.url.com/image.‌​jpg%22,%22myprice%22:550,%22mercname%22:%22hello%22,%22mybool%22:false} 

我想您的URL包含类似于
index.php?call=getUsers&something=bla

要解决此问题,您可以使用
NameValuePair

String url = "http://example.com/index.php";

ArrayList<NameValuePair> nvp = new ArrayList<NameValuePair>();
nvp.add(new BasicNameValuePair("call", "getUsers"));
nvp.add(new BasicNameValuePair("something", "bla"));

try {
    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(url);
    post.setEntity(new UrlEncodedFormEntity(nvp));
    HttpResponse response = client.execute(post);
    HttpEntity entity = response.getEntity();
    [...]
} catch (Exception e) {
    [...]
}
stringurl=”http://example.com/index.php";
ArrayList nvp=新的ArrayList();
添加(新的BasicNameValuePair(“调用”、“获取用户”);
nvp.add(新的BasicNameValuePair(“某物”、“bla”);
试一试{
HttpClient=new DefaultHttpClient();
HttpPost=新的HttpPost(url);
post.setEntity(新的UrlEncodedFormEntity(nvp));
HttpResponse response=client.execute(post);
HttpEntity=response.getEntity();
[...]
}捕获(例外e){
[...]
}
public void postData(){
//创建一个新的HttpClient和Post头
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://www.yoursite.com/script.php");
试一试{
//添加您的数据
List nameValuePairs=新的ArrayList(2);
添加(新的BasicNameValuePair(“id”,“12345”);
添加(新的BasicNameValuePair(“stringdata”,“AndDev很酷!”);
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
//执行HTTP Post请求
HttpResponse response=httpclient.execute(httppost);
}捕获(客户端协议例外e){
//TODO自动生成的捕捉块
}捕获(IOE异常){
//TODO自动生成的捕捉块
}
}

您可以尝试使用以下代码。你应该有


你正在使用的url在哪里?我不能透露url。它是官方的。这是一个API。当我在浏览器中手动输入相同的url时,它可以正常工作。那么如何验证您的代码?@imran我添加了示例url实际上我的url中有一个很长的jsonobject,如{“merchantPrice”:“7.69”,“merchantName”:“amazon.com”}。实际url有如此多的json参数,如下所示。这行得通吗?实际上我的url中有一个很长的jsonobject,如{“merchantPrice”:“7.69”,“merchantName”:“amazon.com”}。实际url有如此多的json参数,如下所示。这行得通吗?你可以试试我下面的答案。我的json解析器工作得很好。我的疑问是我有一个冗长的请求。这就是问题所在。反应很小。这不是问题。我希望冗长的URL不应该成为问题。如果您对请求URL有任何问题,请与我们分享。我会尽力帮助你的。给你。当我手动输入url时,它工作正常。{%22采购员%22:%22http://url.com/logo.png%22,%22price%22:599,%22productDesc%22:%22Apple%2032GBBlack%22,%22prodID%22:%22913393%22,%22merchant%22:%224536%22,%22prourl%22:%22http://url.com/data%22,%22name%22:%22Apple%2032GB%20%2D%20Black%22%22productUrl%22:%22http://www.url.com/image.jpg%22,%22myprice%22:550,%22mercname%22:%22hello%22,%22mybool%22:false}
public void postData() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");

try {
    // Add your data
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
    nameValuePairs.add(new BasicNameValuePair("id", "12345"));
    nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

    // Execute HTTP Post Request
    HttpResponse response = httpclient.execute(httppost);

} catch (ClientProtocolException e) {
    // TODO Auto-generated catch block
} catch (IOException e) {
    // TODO Auto-generated catch block
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.nio.charset.Charset;

import org.json.JSONException;
import org.json.JSONObject;

public class JsonReader {

  private static String readAll(Reader rd) throws IOException {
    StringBuilder sb = new StringBuilder();
    int cp;
    while ((cp = rd.read()) != -1) {
  sb.append((char) cp);
}
return sb.toString();
  }

  public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
    InputStream is = new URL(url).openStream();
    try {
      BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
  String jsonText = readAll(rd);
  JSONObject json = new JSONObject(jsonText);
  return json;
} finally {
  is.close();
    }
  }

  public static void main(String[] args) throws IOException, JSONException {
    JSONObject json = readJsonFromUrl("https://graph.facebook.com/19292868552");
    System.out.println(json.toString());
    System.out.println(json.get("id"));
  }