package com.example.method;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
public class HandMessage {
private static String a="LW";
public static ArrayList<String> queryStringForPost(String url,String method) throws IOException{
URL postUrl = new URL(url);
HttpURLConnection connection = (HttpURLConnection) postUrl
.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
connection.setChunkedStreamingMode(5);
connection.connect();
DataOutputStream out = new DataOutputStream(connection
.getOutputStream());
String mthd = method.subSequence(0, 2).toString();
String ext="";
if(method.length()>2)
{
ext=method.substring(2).toString();
}
String content = mthd;
out.writeBytes(content);
out.flush();
out.close();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
out.flush();
out.close();
String line;
String s="zhbd";
List<String> list = new ArrayList<String>();
System.out.println("GetData~~~~~~~~~~~~~~~~~~~~~~~~~");
while ((line = reader.readLine()) != null) {
System.out.println(line);
s+=line;
list.add(line);
}
System.out.println("GetData~~~~~~~~~~~~~~~~~~~~~~~~~");
reader.close();
connection.disconnect();
a=s;
return (ArrayList<String>) list;
}
public String getA()
{
return a;
}
}
由于
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
出现了异常,导致没有读取到list里面数据。从而程序报错。。。求大神帮我。。
下面是logcat
06-13 16:49:05.506: W/System.err(13029): java.io.FileNotFoundException: http://10.0.2.2:8080/androidWeb/servlet/loadMessage
06-13 16:49:05.516: W/System.err(13029): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)
06-13 16:49:05.516: W/System.err(13029): at com.example.method.HandMessage.queryStringForPost(HandMessage.java:43)
06-13 16:49:05.516: W/System.err(13029): at com.example.method.Runhandle.run(Runhandle.java:23)
06-13 16:49:05.516: W/System.err(13029): at java.lang.Thread.run(Thread.java:856)
06-13 16:49:05.616: W/dalvikvm(13029): threadid=12: thread exiting with uncaught exception (group=0x416fd498)
06-13 16:49:05.616: E/AndroidRuntime(13029): FATAL EXCEPTION: Thread-4837
06-13 16:49:05.616: E/AndroidRuntime(13029): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
06-13 16:49:05.616: E/AndroidRuntime(13029): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)