当前位置: 首页 > 知识库问答 >
问题:

Android API集成[复制]

丰智
2023-03-14

有人能说我怎么能得到响应从API使用HttpHandler方法通过给头参数?这是我的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-

共有1个答案

蓬思博
2023-03-14

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

即追加?参数1

因此,在您的情况下,它将类似于:

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

下面是如何使用这些tow类:

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

祝你好运

 类似资料:
  • http://ehcache.org/generated/2.9.0/html/ehc-all/#page/ehcache_documentation_set%2fco-use_supported_types.html%23wwconnect_header EHCache2.9中的这篇文档说明它将支持RMI、JGroups和JMS。但是,很明显,Ehcache-2.9 JAR中的API已经改变,文

  • 我是Java新手,正在研究一些技术,我想知道是否有可能集成JSF、Spring和PrimeFaces。我正在寻找一些提示,但我只找到了JSF+Spring或Spring+Primefaces或Spring+JSF或JSF+Primefaces,但从来没有同时找到所有3个。有可能把它们都整合在一起? Att, 佩德罗·恩里克

  • 我有一个如下的集成,我从rest控制器调用这个方法,但回复超时并没有像我预期的那样工作。 我期望的是:如果在我给出的回复超时时间内没有响应,则返回timeout作为对客户端的响应。 对于通道配置中的超时持续时间,是否需要执行一些操作? 谢谢。

  • 问题内容: 我的.emacs中有(cua-mode t),因此Cc是复制的,而Cv是粘贴的,就像桌面上的其他大多数程序(Ubuntu,Gnome,Linux)一样。但是,Emacs似乎并未与其他程序共享剪贴板/复制缓冲区。 例如,如果我在Firefox中使用Cc,则可以将SCv粘贴到终端中,或者将Cv粘贴到gedit中。但是,如果我在emacs中使用Cv(或Cy),则无法获得从Firefox复制的

  • 可运行和可调用 如果你在Runnable或Callable中包含你的逻辑,就可以将这些类包装在他们的Sleuth代表中。 Runnable的示例: Runnable runnable = new Runnable() { @Override public void run() { // do some work } @Override public String toString()

  • Jinja2 提供了一些代码来继承到其它工具,诸如框架、 Babel 库或你偏好的编辑器 的奇特的代码高亮。这里是包含的这些的简要介绍。 帮助继承的文件在 这里 可 用。 Babel 集成 Jinja 提供了用 Babel 抽取器从模板中抽取 gettext 消息的支持,抽取器的接入点 名为 jinja2.ext.babel_extract 。 Babel 支持的被作为 i18n 扩展 的 一部分