誰でも私に言うことができますどのように私はAPIからHttpHandlerメソッドを使用してヘッダパラメータを与えることによって応答を得ることができますか? > ID 2 - - 私は私の上記のHTTPHandlerのJavaファイルであることを追加することができますどのように> IMEI ここに私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();``
}
}`
は、私は2つのヘッダパラメータ 1を追加する必要がありますか? plsは誰もが最も簡単な方法は、あなたのURLの最後にのparamsを追加することで、事前
msdev、私は試しました。しかしまだ私は応答を得ていません – Arun
http url接続を使用すると例外が発生します:android.os.NetworkOnMainThreadException ,,なぜ非同期タスクを使用する必要があるのですか – msdev16