Skip to content Skip to sidebar Skip to footer

Post Data From Android To Php

I have an android application that i using to post data to a website using http post but it is not posting any data to the website. In my android code: postParam

Solution 1:

try this,

HttpClientclient=newDefaultHttpClient();
    HttpPostpost=newHttpPost(ADD URL HERE);
    try {
        List<NameValuePair> name = newArrayList<NameValuePair>();

        name.add(newBasicNameValuePair("latitude", Double.toString(latitude))); 
        name.add(newBasicNameValuePair("longitude", Double.toString(longitude)));

        post.setEntity(newUrlEncodedFormEntity(name));
        HttpResponseresponse= client.execute(post);
        BufferedReaderrd=newBufferedReader(newInputStreamReader(
                response.getEntity().getContent()));
        Stringline="";
        StringBuildersb=newStringBuilder();
        while ((line = rd.readLine()) != null) {
            sb.append(line + "\n");
        }
} catch (Exception e) {

    }

Solution 2:

check below code:

publicstaticArrayList<NameValuePair> arrayPost = new ArrayList<NameValuePair>();

add parameter in above array if array size is 0 then,

if (arrayPost.size() <= 0) {
                arrayPost.add(new BasicNameValuePair("test", "test"));
            }

publicstatic String Call_Http_URL_PostMethod(String url)
            throws ClientProtocolException, IOException {

        String is = null;
        try {

            if (arrayPost.size() <= 0) {
                arrayPost.add(new BasicNameValuePair("test", "test"));
            }

            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(url);
            httppost.setEntity(new UrlEncodedFormEntity(arrayPost, HTTP.UTF_8));
            HttpResponse responce = httpclient.execute(httppost);
            HttpEntity entity = responce.getEntity();
            is = EntityUtils.toString(entity);
            Log.e("post responce----->", "" + is);
        } catch (Exception e) {
            Log.d("post responce error ----->", "" + e.getMessage().toString());
            is = null;
        }
        returnis;
    }

change your php code like below(this is just for testing)

if($result)
{
echo json_encode("result"=array("sucsess"););
}

echo json_encode("result"=array("Database Error"));

Post a Comment for "Post Data From Android To Php"