gpt4 book ai didi

android - java.lang.NullPointerException 在 HttpGet httpGet = new HttpGet(url);

转载 作者:行者123 更新时间:2023-11-29 17:43:58 25 4
gpt4 key购买 nike

公共(public)类 ServiceHandler {

static String response = null;
public final static int GET = 1;
public final static int POST = 2;

public ServiceHandler() {

}

/*
* Making service call
* @url - url to make request
* @method - http request method
* */
public String makeServiceCall(String url, int method) {
return this.makeServiceCall(url, method, null);
}

/*
* Making service call
* @url - url to make request
* @method - http request method
* @params - http request params
* */
public String makeServiceCall(String url, int method,
List<NameValuePair> params) {
try {
// http client
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpEntity httpEntity = null;
HttpResponse httpResponse = null;

// Checking http request method type
if (method == POST) {
HttpPost httpPost = new HttpPost(url);
httpResponse = httpClient.execute(httpPost);

} else if (method == GET) {

HttpGet httpGet = new HttpGet(url);
httpResponse = httpClient.execute(httpGet);

}
httpEntity = httpResponse.getEntity();
response = EntityUtils.toString(httpEntity);

} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

return response;

}

这是我用来从 REST 请求中获取字符串的类

    12-26 10:51:35.610    1750-1778/hackerspace.invento.ebooks E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #2
Process: hackerspace.invento.ebooks, PID: 1750
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
Caused by: java.lang.NullPointerException
at java.net.URI.parseURI(URI.java:353)
at java.net.URI.<init>(URI.java:204)
at java.net.URI.create(URI.java:725)
at org.apache.http.client.methods.HttpGet.<init>(HttpGet.java:75)
at hackerspace.invento.ebooks.ServiceHandler.makeServiceCall(ServiceHandler.java:63)
at hackerspace.invento.ebooks.ServiceHandler.makeServiceCall(ServiceHandler.java:39)
at hackerspace.invento.ebooks.Books$GetResult.doInBackground(Books.java:113)
at hackerspace.invento.ebooks.Books$GetResult.doInBackground(Books.java:94)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)

在 android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)在 java.lang.Thread.run(Thread.java:841)

这是我得到的日志错误 我该如何解决这个问题?

最佳答案

我的猜测是,您要么将 NULL 字符串传递到您的方法中,要么由于某种原因,如果 HttpGet () 方法无法解析 URL 或其他错误。尝试在 try/catch 语句的末尾添加:

catch (Exception e)
{
Log.e("MyApp", "exception: " + e.getMessage());
Log.e("MyApp", "exception: " + e.toString());
}

通常,在您的软件中使用这样的语句是不好的做法,因为它基本上会压缩您获得的任何异常,而不允许您处理各种异常情况。但是,出于调试目的,在 try/catch 语句的末尾添加此语句将允许您查看是否可能存在您遗漏的异常导致HttpGet() 方法失败,进而导致您的 NULL 错误。如果您检测到另一个异常,您可以适本地处理它,以便您的方法不再返回 NULL 字符串。

关于android - java.lang.NullPointerException 在 HttpGet httpGet = new HttpGet(url);,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27659229/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com