2016-12-02 5 views
0

누구나 내가 어떻게 API에서 헤더 매개 변수를 제공하여 HttpHandler 메서드를 사용하여 응답을 얻을 수 말할 수 있을까? 여기안드로이드 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();`` 
    } 
}` 

내가 두 개의 헤더 매개 변수를 추가 할 필요가 내 HttpHandler를 자바 code`입니다 1 -> 아이디 2 -> IMEI 내가 추가 할 수있는 방법이 내 위의 HttpHandler를 자바 파일에? PLS 사람이 사전에 me.Many 덕분에 도움이

답변

0

가장 간단한 방법은 당신의 URL의 끝에 PARAMS을 추가하는 것입니다?

즉 추가 PARAM1 = 값 1 & PARAM2 = 귀하의 경우

때문에 값 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 ,, and thats async task – msdev16