掃描器類返回InputStream對象時提供一個構造函數的InputStream作爲參數java的掃描儀對象拋出嘗試讀取從HTTP請求
所以我創建一個HttpResponse對象,並使用getEntity一個NoSuchElement異常()方法獲取HttpEntity,然後調用實體上的getContent()以獲取InputStream對象。所以我試圖使用掃描儀來讀取返回的InputStream對象,但它失敗。 我遇到它執行一些查詢到MySQL數據庫,並使用下面的語句使呼應的結果
PS ,我有一個PHP腳本本地託管試圖使用HTTPGET請求連接到數據庫在我的Android應用程序時,這個問題我從數據庫中沒有任何問題
BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
獲取的結果,但我只是好奇,爲什麼掃描儀是行不通的,我想成爲一個偉大的開發者,所以每當我遇到這樣一個問題,我試着去了解爲什麼會出現問題。
這裏的的AsyncTask
package com.example.helloworld;
import android.content.Context;
import android.os.AsyncTask;
import android.widget.TextView;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URL;
import java.util.Scanner;
public class SignIn extends AsyncTask<String,Void,String> {
private TextView roleField;
private Context context;
private int byGetOrPost= 0;
public SignIn(Context context,TextView roleField,int flag)
{
this.context=context;
this.roleField = roleField;
byGetOrPost = flag;
}
@Override
protected String doInBackground(String... params)
{
if(byGetOrPost == 0)
{
try
{
String username = params[0];
String password=params[1];
String link = "http://10.0.2.2/myandroidapp/create.php?username="+username+"&password="+password;
URL url = new URL(link);
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI(link));
HttpResponse response = client.execute(request);
Scanner in = new Scanner(response.getEntity().getContent());
//BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line ="";
while((line=in.nextLine())!=null)
{
sb.append(line);
}
in.close();
return sb.toString();
}
catch (Exception e)
{
e.printStackTrace();
return new String("Exception: " + e.getMessage());
}
}
else
{
try
{
return null;
}
catch (Exception e)
{
return new String("Exception: " + e.getMessage());
}
}
}
@Override
protected void onPostExecute(String result)
{
this.roleField.setText(result);
}
}
我的代碼片段時,我使用Scanner對象,NoSuchElement拋出異常。這裏是堆棧跟蹤
08-23 10:54:05.460 2188-2207/com.example.helloworld W/System.err﹕ java.util.NoSuchElementException
08-23 10:54:05.460 2188-2207/com.example.helloworld W/System.err﹕ at java.util.Scanner.nextLine(Scanner.java:1363)
08-23 10:54:05.460 2188-2207/com.example.helloworld W/System.err﹕ at com.example.helloworld.SignIn.doInBackground(SignIn.java:54)
08-23 10:54:05.460 2188-2207/com.example.helloworld W/System.err﹕ at com.example.helloworld.SignIn.doInBackground(SignIn.java:20)
08-23 10:54:05.460 2188-2207/com.example.helloworld W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:292)
08-23 10:54:05.460 2188-2207/com.example.helloworld W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
08-23 10:54:05.460 2188-2207/com.example.helloworld W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
08-23 10:54:05.460 2188-2207/com.example.helloworld W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
08-23 10:54:05.460 2188-2207/com.example.helloworld W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
08-23 10:54:05.460 2188-2207/com.example.helloworld W/System.err﹕ at java.lang.Thread.run(Thread.java:818)
當我擺脫掃描儀,並使用BufferedReader代替,它的工作原理。 Of循環在while循環中,當我使用BufferedReader讀取內容時,我將「in.nextLine()」更改爲「in.readLine()」。它返回正確的結果從數據庫
BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
我的問題是,爲什麼
BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
工作,但
Scanner in = new Scanner(response.getEntity().getContent());
不起作用進賬?
在此先感謝
請發佈失敗的代碼。 – PhilMasterG
'失敗'不是問題描述。 – EJP
不夠。仍然沒有代碼,也沒有堆棧跟蹤。沒有'空對象異常'這樣的事情,但有一個'NullPointerException'。但它不是'返回',而是拋出。你到底在說什麼? – EJP