【发布时间】:2016-12-02 16:58:47
【问题描述】:
谁能告诉我如何通过提供标头参数来使用 HttpHandler 方法从 API 获取响应?这是我的 Httphandler java 代码`
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 -> 身份证 2 -> imei 如何在我上面的 httphandler java 文件中添加它?请任何人帮助我。非常感谢提前
【问题讨论】:
标签: android