oliver
oliver

Reputation: 39

how pass String parameter to url

i need to pass to this URL a parameter http://192.168.1.15:8888/android_login_api/getsPreferiti.php?id="+mParam1

where mParam1 contains this String 5a325bc1b214c5.50816853

how can i do?

PS:now i get this: Response from url: {"error":false,"message":"VIDEOs fetched successfully.","pdfs":[]} but pdfs array have pdfs

Upvotes: 1

Views: 8007

Answers (2)

Eli Zhang
Eli Zhang

Reputation: 44

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

And you can call this method by using AsyncTask as follow:

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

Upvotes: 0

Ravi
Ravi

Reputation: 940

Add parameters to HTTPURL Connection using HTTPPost

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

Upvotes: 1

Related Questions