首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android API集成

Android API集成
EN

Stack Overflow用户
提问于 2016-12-02 15:54:22
回答 1查看 132关注 0票数 0

有没有人能告诉我,如何通过给出头部参数,使用HttpHandler方法从接口获取响应?这是我的Httphandler java代码`

代码语言:javascript
复制
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();``
    }
}`

我需要添加两个头参数1httphandler id 2httphandler imei如何在上面的-> java文件中添加它?请任何人帮助me.Many提前感谢

EN

回答 1

Stack Overflow用户

发布于 2016-12-02 16:24:19

最简单的方法是将参数添加到url的末尾:

即追加?param1=value1¶m2=value2

因此,在您的情况下,应该是这样的:

代码语言:javascript
复制
URL url = new URL("http://garage.kaptastech.mobi/api/5k/users/vehicle?id=[id]&imei=[imei]");

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

编辑:如果您正在尝试获取响应,您可以使用异步任务,请参阅本教程,其中使用HttpHandler和异步任务:

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

代码语言:javascript
复制
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();
} 
}

这是异步任务

代码语言:javascript
复制
 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;   
    }
}

下面是如何使用这两个类:

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40927267

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档