在下面的代码中,我试图对我在 localhost 上编码和托管的 web 服务执行 GET 操作。OpenHttpConnection 方法工作得很好,因为我在中间放了祝酒词来检查那里是否有错误。当我尝试使用 bufferreader 将输入流转换为字符串时,应用程序崩溃。请看一下,看看你是否能发现错误。
谢谢 :)
public class ServicetestActivity extends Activity {
public static String iStream_to_String(InputStream is1)
{
BufferedReader rd = new BufferedReader(new InputStreamReader(is1));
String line;
StringBuilder sb = new StringBuilder();
try {
while ((line = rd.readLine()) != null) {
sb.append(line);
}
rd.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String contentOfMyInputStream = sb.toString();
return contentOfMyInputStream;
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
InputStream is=null;
try {
is=OpenHttpConnection("http://localhost/webservice.php?device=ayaz");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String line= iStream_to_String(is);
Toast.makeText(this, line, Toast.LENGTH_LONG).show();
}
private InputStream OpenHttpConnection(String link) throws IOException {
// TODO Auto-generated method stub
InputStream inputStream = null;
int response = -1;
Toast.makeText(this, "1", Toast.LENGTH_SHORT).show();
URL url = new URL(link);
URLConnection connection = url.openConnection();
Toast.makeText(this, "2", Toast.LENGTH_SHORT).show();
if (!(connection instanceof HttpURLConnection))
throw new IOException("Not a HTTP connection");
try {
HttpURLConnection httpURLConnection = (HttpURLConnection) connection;
Toast.makeText(this, "3", Toast.LENGTH_SHORT).show();
httpURLConnection.setAllowUserInteraction(false);
Toast.makeText(this, "4", Toast.LENGTH_SHORT).show();
httpURLConnection.setInstanceFollowRedirects(true);
httpURLConnection.setRequestMethod("GET");
httpURLConnection.connect();
response = httpURLConnection.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) {
inputStream = httpURLConnection.getInputStream();
}
} catch (Exception e) {
// TODO: handle exception
throw new IOException("Error connecting");
}
return inputStream;}
// see http://androidsnippets.com/executing-a-http-get-request-with-httpclient
}
日志猫也如下:
10-14 20:54:13.660: E/AndroidRuntime(1348): FATAL EXCEPTION: main
10-14 20:54:13.660: E/AndroidRuntime(1348): java.lang.RuntimeException: Unable to start activity ComponentInfo{web.service/web.service.ServicetestActivity}: java.lang.NullPointerException
10-14 20:54:13.660: E/AndroidRuntime(1348): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
10-14 20:54:13.660: E/AndroidRuntime(1348): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
10-14 20:54:13.660: E/AndroidRuntime(1348): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-14 20:54:13.660: E/AndroidRuntime(1348): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
10-14 20:54:13.660: E/AndroidRuntime(1348): at android.os.Handler.dispatchMessage(Handler.java:99)
10-14 20:54:13.660: E/AndroidRuntime(1348): at android.os.Looper.loop(Looper.java:130)
10-14 20:54:13.660: E/AndroidRuntime(1348): at android.app.ActivityThread.main(ActivityThread.java:3683)
10-14 20:54:13.660: E/AndroidRuntime(1348): at java.lang.reflect.Method.invokeNative(Native Method)
10-14 20:54:13.660: E/AndroidRuntime(1348): at java.lang.reflect.Method.invoke(Method.java:507)
10-14 20:54:13.660: E/AndroidRuntime(1348): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-14 20:54:13.660: E/AndroidRuntime(1348): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-14 20:54:13.660: E/AndroidRuntime(1348): at dalvik.system.NativeStart.main(Native Method)
10-14 20:54:13.660: E/AndroidRuntime(1348): Caused by: java.lang.NullPointerException
10-14 20:54:13.660: E/AndroidRuntime(1348): at java.io.Reader.<init>(Reader.java:65)
10-14 20:54:13.660: E/AndroidRuntime(1348): at java.io.InputStreamReader.<init>(InputStreamReader.java:122)
10-14 20:54:13.660: E/AndroidRuntime(1348): at java.io.InputStreamReader.<init>(InputStreamReader.java:59)
10-14 20:54:13.660: E/AndroidRuntime(1348): at web.service.ServicetestActivity.iStream_to_String(ServicetestActivity.java:31)
10-14 20:54:13.660: E/AndroidRuntime(1348): at web.service.ServicetestActivity.onCreate(ServicetestActivity.java:65)
10-14 20:54:13.660: E/AndroidRuntime(1348): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-14 20:54:13.660: E/AndroidRuntime(1348): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
10-14 20:54:13.660: E/AndroidRuntime(1348): ... 11 more