所以我正在制作一个应用程序,我只是初学者,但我被卡住了,我有这样的代码:
public class MenuChoice extends Activity {
InputStream string = null;
ArrayList<String> Deals = null;
ArrayList<String> ImageBase;
int currentDeal = 0;
public void StreamReader() throws IOException{
InputStreamReader is = new InputStreamReader(string);
StringBuilder sb=new StringBuilder();
BufferedReader br = new BufferedReader(is);
String read = br.readLine();
while(read != null) {
//System.out.println(read);
sb.append(read);
read =br.readLine();
}
String[] DealList = sb.toString().split("±");
//ArrayList<String> deals = new ArrayList<String>();
for(int i = 0; i < DealList.length; i++){
Log.d("msgs", DealList[i]);
if(DealList[i].length() > 5){
Deals.add(DealList[i]);
}
}
// return deals;
}
public String DownloadImg(String urla, String newname){
String fileName = null;
try{
URL url = new URL (urla);
InputStream input = url.openStream();
try {
//The sdcard directory e.g. '/sdcard' can be used directly, or
//more safely abstracted with getExternalStorageDirectory()
File storagePath = Environment.getExternalStorageDirectory();
OutputStream output = new FileOutputStream (storagePath + "/" + newname + "/.png");
fileName = storagePath + "/" + newname + "/.png";
try {
byte[] buffer = new byte[5000];
int bytesRead = 0;
while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) {
output.write(buffer, 0, bytesRead);
}
} finally {
output.close();
}
} finally {
input.close();
}
}
catch(Exception e){
}
return fileName;
}
public void SetInp(InputStream inp){
string = inp;
}
public class GetDeals extends AsyncTask<Void, Void, Void>{
@Override
protected Void doInBackground(Void... params) {
InputStream str = null;
HttpClient htc = new DefaultHttpClient();
HttpPost htp = new HttpPost("site/android_connect.php");
try {
HttpEntity resp = htc.execute(htp).getEntity();
InputStream strings = resp.getContent();
str = strings;
} catch (ClientProtocolException e) {
Log.d("problema" ,"BAD!");
e.printStackTrace();
} catch (IOException e) {
Log.d("problema" ,"BAD!");
e.printStackTrace();
}
string = str;
return null;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu_choice);
try {
new GetDeals().execute().get();
}
catch(Exception e){}
/* try {
Deals = StreamReader(string);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/*
for(int i = 0; i<Deals.size(); i++){
ImageBase.add(DownloadImg(Deals.get(i), "Deal"+i));
Log.d("bugger", DownloadImg(Deals.get(i), "Deal"+i));
}
*/
try {
StreamReader();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_choice, menu);
return true;
}
}
所以我抛出了这个错误:
08-26 17:42:52.030:E/AndroidRuntime(10221):致命异常:main08-26 17:42:52.030:E/AndroidRuntime(10221):java.lang.RuntimeException:无法启动活动组件信息{plugMedia.kcreea/plugMedia.kcreEA.MenuChoice}:android.os.NetworkOnMainThreadException 08-26 17:42:52.030:E/AndroidRuntime(10221):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)08-26 17:42:52.030:E/AndroidRuntime(10221):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)08-26 17:42:52.030:E/AndroidRuntime(10221):在android.app.ActivityThread.access$600(ActivityThreads.java:123)08-2617:42:52.030:E/AndroidRuntime(10221):在android.app.ActivityThread$H.handleMessage(ActivityThreads.java:1147)08-26 17:42:52.030:E/AndroidRuntime(10221):在android.os.Handler.dispatchMessage(Handler.java:99)08-2617:42:52.030:E/AndroidRuntime(10221):在android.os.Looper.loop(Looper.java:137)08-26 17:42:52.030:E/AndroidRuntime(10221):在android.app.ActivityThread.main(ActivityThreads.java:4424)08-2617:42:52.030:E/AndroidRuntime(10221):在java.lang.reflect.Method.invokeNative(Native Method)08-2617:42:52.030:E/AndroidRuntime(10221):在java.lang.reflect.Method.ioke(Method.java:511)08-26 17:42:52.030:E/AndroidRuntime(10221):在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)08-26 17:42:52.030:E/AndroidRuntime(10221):在com.android.internal.os.ZygoteInit.main(ZygoteNit.java:554)08-2617:42:52.030:E/AndroidRuntime(10221):在dalvik.system.NativeStart.main(本地方法)08-26 17:42:52.030:E/AndroidRuntime(10221):由以下原因引起:android.os.NetworkOnMainThreadException 08-26 17:42:52.030:E/AndroidRuntime(10221):在android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)08-26 17:42:52.030:E/AndroidRuntime(10221):在libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:163)08-2617:42:52.030:E/AndroidRuntime(10221):在libcore.io.IoBridge.recvfrom(IoBridge.java:503)08-26 17:42:52.030:E/AndroidRuntime(10221):在java.net.PlainSocketImpl.read(PlainSocketImpl.java:488)08-2617:42:52.030:E/AndroidRuntime(10221):在java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)08-2617:42:52.030:E/AndroidRuntime(10221):在java.net.PlainSocketImpl$PlainSocketInputStream.read(PlasSocketImpl.java:240)08-26 17:42:52.030:E/AndroidRuntime(10221):在org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSession输入缓冲区.java:103)08-26 17:42:52.030:E/AndroidRuntime(10221):在org.apache.http.impl.io.AbstractSessionInputBuffer.read(AbstractSession输入缓冲区.java:120)08-26 17:42:52.030:E/AndroidRuntime(10221):在org.apache.http.impl.io.ChunkedInputStream.getChunkSize(ChunkedInput Stream.java:211)08-26 17:42:52.030:E/AndroidRuntime(10221):在org.apache.http.impl.io.ChunkedInputStream.nextChunk(ChunkedInput Stream.java:183)08-26 17:42:52.030:E/AndroidRuntime(10221):在org.apache.http.impl.io.ChunkedInputStream.read(ChunkedInput Stream.java:155)08-26 17:42:52.030:E/AndroidRuntime(10221):在org.apache.http.con.ESensorInputStream.read(EofSensorInputStream.java:159)08-26 17:42:52.030:E/AndroidRuntime(10221):在java.io.InputStreamReader.read(InputStreamReader.java:244)08-2617:42:52.030:E/AndroidRuntime(10221):在java.io.BufferedReader.fillBuf(BufferedReader.java:130)08-2617:42:52.030:E/AndroidRuntime(10221):在java.io.BufferedReader.readLine(BufferedReader.java:390)08-2617:42:52.030:E/AndroidRuntime(10221):在plugMedia.kcreea.MenuChoice.StreamReader(MenuChoice.java:59)08-2617:42:52.030:E/AndroidRuntime(10221):在plugMedia.kcreea.MenuChoice.onCreate(MenuChoice.java:158)08-2617:42:52.030:E/AndroidRuntime(10221):在android.app.Activity.performCreate(Activity.java:4465)08-2617:42:52.030:E/AndroidRuntime(10221):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)08-26 17:42:52.030:E/AndroidRuntime(10221):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)08-26 17:42:52.030:E/AndroidRuntime(10221):。。。11个
我知道这是说我试图在主线程上执行Network操作,但我所做的只是调用StreamReader,我认为它只是转换InputStream(从AsyncTask保存和检索,但它会抛出错误。
有什么帮助吗?是什么原因造成的?
谢谢。
但如果我移除.get(),它不会将InputSream字符串=null设置为它从链接中获得的InputStream。
将您对InputStream
的使用及其结果(例如,您的StreamReader()
调用)移动到AsyncTask
的onPostExecute()
,并删除对get()
的调用。正如Raghunadan所指出的,get()
基本上意味着首先使用AsyncTask
毫无意义,这就是为什么我有一个出色的功能请求,要求为此添加Lint警告。