2016-12-02 88 views
0

任何人都可以告訴我如何通過給頭參數使用HttpHandler方法從API獲取響應?這是我的HttpHandler的java code`Android API集成

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

我需要添加兩個頭參數 1 - > ID 2 - > IMEI 如何添加,在我上面的HttpHandler的Java文件?請人幫助me.Many在此先感謝

回答

0

最簡單的方法是將PARAMS添加到URL的末尾:?

即追加參數1 =值1 &參數2 =值2

所以你的情況它會是這樣的:

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

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

編輯:如果你是試圖讓你可以在本教程中,它使用的HttpHandler和異步任務使用異步任務 看的響應:

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

,這是異步任務

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

這裏是你如何使用這些兩類:

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

祝你好運!

+0

msdev,我試過了。但仍然沒有得到任何迴應 – Arun

+0

您使用http url連接導致異常:android.os.NetworkOnMainThreadException,這就是爲什麼你需要使用異步任務 – msdev16