如何将String参数传递给url

时间:2017-12-21 14:40:30

标签: java android

我需要将参数http://192.168.1.15:8888/android_login_api/getsPreferiti.php?id="+mParam1

传递给此网址

其中mParam1包含此字符串5a325bc1b214c5.50816853

我该怎么办?

PS:现在我明白了:来自网址的响应:{"错误":false,"消息":" VIDEO成功获取。",&# 34; pdfs":[]}但pdfs数组有pdfs

2 个答案:

答案 0 :(得分:1)

使用HTTPPost将参数添加到HTTPURL连接

URL url = new URL("http://yoururl.com");
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setReadTimeout(10000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);

List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("parameter1", parameterValue1));
params.add(new BasicNameValuePair("parameter2", parameterValue2));

OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
    new OutputStreamWriter(os, "UTF-8"));
writer.write(getQuery(params));
writer.flush();
writer.close();
os.close();
conn.connect();

private String getQuery(List<NameValuePair> params) throws 
UnsupportedEncodingException
{
  StringBuilder result = new StringBuilder();
  boolean first = true;

  for (NameValuePair pair : params)
   {
    if (first)
        first = false;
    else
        result.append("&");

    result.append(URLEncoder.encode(pair.getName(), "UTF-8"));
    result.append("=");
    result.append(URLEncoder.encode(pair.getValue(), "UTF-8"));
   }

    return result.toString();
}

答案 1 :(得分:0)

public String makeServiceCall(String url, int method,
                                     List<NameValuePair> params) {

    String response = null;
    int GET = 1;
    int POST = 2;
    try {
        // http client
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpEntity httpEntity = null;
        HttpResponse httpResponse = null;

        // Checking http request method type
        if (method == POST) {
            HttpPost httpPost = new HttpPost(url);

            // adding post params
            if (params != null) {
                httpPost.setEntity(new UrlEncodedFormEntity(params));
            }
            httpResponse = httpClient.execute(httpPost);
        } else if (method == GET) {
            // appending params to url
            if (params != null) {
                String paramString = URLEncodedUtils
                        .format(params, "utf-8");
                url += "?" + paramString;
            }
            HttpGet httpGet = new HttpGet(url);
            httpResponse = httpClient.execute(httpGet);
        }
        httpEntity = httpResponse.getEntity();
        response = EntityUtils.toString(httpEntity);

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

    return response;
}

您可以使用AsyncTask调用此方法,如下所示:

try {
    JSONObject jData = new JSONObject();
    String mParam = "5a325bc1b214c5.50816853";
    jData.put("id", mParam);

    List<NameValuePair> params1 = new ArrayList<NameValuePair>(2);
    params1.add(new BasicNameValuePair("data", jData.toString()));
    response = makeServiceCall("http://192.168.1.15:8888/android_login_api/getsPreferiti.php", 1, params1);        
} catch (JSONException e) {
    e.printStackTrace();
}