Skip to content Skip to sidebar Skip to footer

Android And Php Creating An Api

I'm trying to set up a PHP API for my Android application to interact with, my problem is that the post data never seems to get posted and I can never retrieve the response body (H

Solution 1:

public String getResponse(String url, List<NameValuePair> nameValuePairs) {

    url = url.replaceAll(" ", "%20");
    Stringresult="ERROR";
    Log.d("Pair", nameValuePairs.toString());
    HttpClienthttpclient=newDefaultHttpClient();
    HttpParamshttp_params= httpclient.getParams();
    http_params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION,
            HttpVersion.HTTP_1_1);
    HttpConnectionParams.setConnectionTimeout(http_params, TIMEOUT);
    HttpConnectionParams.setSoTimeout(http_params, TIMEOUT);
    HttpResponseresponse=null;

    try {
        HttpPosthttppost=newHttpPost(url);
        httppost.setEntity(newUrlEncodedFormEntity(nameValuePairs));
        response = httpclient.execute(httppost);
    } catch (ClientProtocolException e) {
        e.printStackTrace();
        result = "ERROR";
    } catch (IOException e) {
        e.printStackTrace();
        result = "ERROR";
    }

    try {
        // Get hold of the response entityHttpEntityentity=null;
        if (response != null) {
            entity = response.getEntity();
        }
        if (entity != null) {
            InputStreaminputStream= entity.getContent();
            result = convertStreamToString(inputStream);
        }

    } catch (IOException e) {
        result = "ERROR";
    }

    httpclient.getConnectionManager().shutdown();
    return result;
}

Use this Method with AsyncTask or background thread

Solution 2:

This one is working for me. Please do take care of exceptions ans error handling. Key121 variable is your php file url.

classcallServiceTaskextendsAsyncTask<Void, Void, Void>
{
    @Overrideprotected Void doInBackground(Void... params) {
    loginCheck();
    returnnull;
    }
} 
publicvoidloginCheck()
{
InputStreamis=null;
ArrayList<NameValuePair> nameValuePairs = newArrayList<NameValuePair>();
nameValuePairs.add(newBasicNameValuePair("aa","11"));   
nameValuePairs.add(newBasicNameValuePair("bb","22"));

    try{
        HttpClienthttpclient=newDefaultHttpClient();
        HttpPosthttppost=newHttpPost(KEY_121);
        httppost.setEntity(newUrlEncodedFormEntity(nameValuePairs));
        HttpResponseresponse= httpclient.execute(httppost);
        HttpEntityentity= response.getEntity();
        is = entity.getContent();
    }catch(Exception e)
    {
        Log.e("log_tag", "Error:"+e.toString());
    }
//convert response to stringtry{
    BufferedReaderreader=newBufferedReader(newInputStreamReader(is,"iso-8859-1"),8);
    StringBuildersb=newStringBuilder();
    Stringline=null;
    while ((line = reader.readLine()) != null) {
    sb.append(line + "\n");
    }
    is.close();
    result=sb.toString();
    Log.e("log_tag", "----------------"+result);
    }catch(Exception e){
    Log.e("log_tag", "Error converting result "+e.toString());
    }
}

You need to use stream and buffered reader for analysing response. Do check and let us know.

Post a Comment for "Android And Php Creating An Api"