0

can anyone say me how can i get the response from the API using HttpHandler method by giving the header parameters? Here is my Httphandler java code`

package com.example.addvehicle;
import android.util.Log;
import android.widget.ListView;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;

import java.net.URL;
public class HttpHandler {
    private static final String TAG = HttpHandler.class.getSimpleName();



    public HttpHandler() {
    }

    public String makeServiceCall(String reqUrl) {
        String response = null;
        try{
            URL url = new URL("http://garage.kaptastech.mobi/api/5k/users/vehicle");

            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            InputStream in = new BufferedInputStream(conn.getInputStream());
            response = convertStreamToString(in);



        }
        catch (MalformedURLException e) {
            Log.e(TAG, "MalformedURLException: " + e.getMessage());
        }catch (ProtocolException e) {
            Log.e(TAG, "ProtocolException: " + e.getMessage());
        } catch (IOException e) {
            Log.e(TAG, "IOException: " + e.getMessage());
        } catch (Exception e) {
            Log.e(TAG, "Exception: " + e.getMessage());
        }
        return response;

    }
    private String convertStreamToString(InputStream is) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();
        String line;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line).append('\n');
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();

            } catch (IOException e) {
                e.printStackTrace();

            }
        }
        return sb.toString();``
    }
}`

i need to add two header parameters 1 -> id 2 -> imei How can i add that in my above httphandler java file? pls anyone help me.Many thanks in advance

Arun
  • 33
  • 8

1 Answers1

0

the simplest way is to add the params to the end of your url :

i.e. append ?param1=value1&param2=value2

so in your case it would be something like :

URL url = new URL("http://garage.kaptastech.mobi/api/5k/users/vehicle?id=[id]&imei=[imei]");

===============================================================

edit : if you are trying to get the response you can use an async task look at this tutorial which uses HttpHandler and async task:

http://hmkcode.com/android-cleaner-http-asynctask/

package com.hmkcode.http;

import org.apache.http.client.methods.HttpUriRequest;
import com.hmkcode.http.AsyncHttpTask;

public abstract class HttpHandler {

public abstract HttpUriRequest getHttpRequestMethod();

public abstract void onResponse(String result);

public void execute(){
    new AsyncHttpTask(this).execute();
} 
}

and this is the async task

 package com.hmkcode.http;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import com.hmkcode.http.HttpHandler;
import android.os.AsyncTask;
import android.util.Log;

public class AsyncHttpTask extends AsyncTask<String, Void, String>{

private HttpHandler httpHandler;
public AsyncHttpTask(HttpHandler httpHandler){
    this.httpHandler = httpHandler;
}

@Override
protected String doInBackground(String... arg0) {
    InputStream inputStream = null;
    String result = "";
    try {

        // create HttpClient
        HttpClient httpclient = new DefaultHttpClient();

        // make the http request
        HttpResponse httpResponse = httpclient.execute(httpHandler.getHttpRequestMethod());

        // receive response as inputStream
        inputStream = httpResponse.getEntity().getContent();

        // convert inputstream to string
        if(inputStream != null)
            result = convertInputStreamToString(inputStream);
        else
            result = "Did not work!";

    } catch (Exception e) {
        Log.d("InputStream", e.getLocalizedMessage());
    }

    return result;
}
@Override
protected void onPostExecute(String result) {
    httpHandler.onResponse(result);
}

//--------------------------------------------------------------------------------------------
 private static String convertInputStreamToString(InputStream inputStream) throws IOException{
        BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
        String line = "";
        String result = "";
        while((line = bufferedReader.readLine()) != null)
            result += line;

        inputStream.close();
        return result;   
    }
}

and here is how you use these tow classes:

new HttpHandler() {
        @Override
        public HttpUriRequest getHttpRequestMethod() {

            return new HttpGet("http://hmkcode.com/examples/index.php");

            // return new HttpPost(url)
        }
        @Override
        public void onResponse(String result) {
            // what to do with result 
            //e.g. display it on edit text etResponse.setText(result);
        }

    }.execute();

good luck !

msdev16
  • 201
  • 3
  • 11
  • msdev, i have tried. but still i didn't get any response – Arun Dec 02 '16 at 08:34
  • the you used http url connection causes an exception : android.os.NetworkOnMainThreadException ,, and thats why you need to use an async task – msdev16 Dec 02 '16 at 09:10