1

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

4
  • Please have a think about rewriting this question with enough information for the community to be able to answer it. Take a look at the guidance here stackoverflow.com/help/how-to-ask Commented Dec 21, 2017 at 14:44
  • How do you want to pass the parameter ? Through HTTP GET request, parameters are sent as a query string and through HTTP Post , they are sent in the request body. Commented Dec 21, 2017 at 14:46
  • @shravani i use httpPost Commented Dec 21, 2017 at 14:54
  • @oliver Please accept the answer Commented Dec 21, 2017 at 15:43

2 Answers 2

1

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();
}
Sign up to request clarification or add additional context in comments.

2 Comments

how can i put this in background?
Use an async task and put the above code in 'doInBackground()'
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;
}

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.