【发布时间】:2015-01-04 10:50:12
【问题描述】:
我想从网站获取字符串,并将其转换为数组。它在 Eclipse 中工作,但如果我想在 Android 应用程序中使用它,它将无法工作。我知道,这一定是个愚蠢的错误,但我不知道它在哪里。
InternetHandler:
package de.blender4me.einkaufsliste;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.regex.Pattern;
public class internetHandler {
//Einkaufsplaner-API aufrufen
public static String[] getList(){
InputStream is = null;
try
{
URL url = new URL("https://einkaufsplaner.blender4me.de/api.php?name=Username&pass=SuperSecretPass&addValue=&removeValue=");
BufferedReader in = new BufferedReader(
new InputStreamReader(url.openStream()));
String Values = null;
for(int i = 0; i < 4; ++i){
Values = in.readLine();
}
in.close();
String[] List = Values.split( Pattern.quote( ";" ) );
return List;
}
catch ( Exception e ) {
e.printStackTrace();
}
finally {
if ( is != null )
try { is.close(); } catch ( IOException e ) { }
}
String[] error = {"error"};
return error;
}
}
主要
package de.blender4me.einkaufsliste;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
public class MainActivity extends Activity {
//Widgets
Button addItem;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//listView
populateListView();
//register Widgets
addItem =(Button) findViewById(R.id.addItem);
addItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openDialog();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
//Alert
public void openDialog(){
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Zur Einkaufsliste hinzufügen");
alert.setMessage(" ");
// Set an EditText view to get user input
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton("Bestätigen", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String value = input.getText().toString();
// Do something with value!
}
});
alert.setNegativeButton("Abbrechen", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.show();
}
//listView erstellen
private void populateListView() {
// Liste auslesen
String[] values = internetHandler.getList();
//Liste konvertieren
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.listview_item, //Genutztes Layout
values); //Genutzter Array
//listView einstellen
ListView list = (ListView) findViewById(R.id.listView);
list.setAdapter(adapter);
}
}
清单
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="19" /> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@android:style/Theme.Holo.Light.NoActionBar.Fullscreen">
<category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>
它总是返回错误数组,但不知道为什么:C
logcat:
11-07 15:18:24.425:E/Trace(642):打开跟踪文件时出错:没有这样的 文件或目录 (2) 11-07 15:18:24.925: W/System.err(642): android.os.NetworkOnMainThreadException 11-07 15:18:24.925: W / System.err(642):在 android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117) 11-07 15:18:24.935: W/System.err(642): 在 java.net.InetAddress.lookupHostByName(InetAddress.java:385) 11-07 15:18:24.935:W / System.err(642):在 java.net.InetAddress.getAllByNameImpl(InetAddress.java:236) 11-07 15:18:24.935:W / System.err(642):在 java.net.InetAddress.getAllByName(InetAddress.java:214) 11-07 15:18:24.935:W / System.err(642):在 libcore.net.http.HttpConnection.(HttpConnection.java:70) 11-07 15:18:24.935:W / System.err(642):在 libcore.net.http.HttpConnection.(HttpConnection.java:50) 11-07 15:18:24.935:W / System.err(642):在 libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:341) 11-07 15:18:24.935: W/System.err(642): 在 libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87) 11-07 15:18:24.935: W/System.err(642): 在 libcore.net.http.HttpConnection.connect(HttpConnection.java:128) 11-07 15:18:24.935:W / System.err(642):在 libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315) 11-07 15:18:24.945: W/System.err(642): 在 libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:461) 11-07 15:18:24.945: W/System.err(642): 在 libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:433) 11-07 15:18:24.945: W/System.err(642): 在 libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289) 11-07 15:18:24.945: W/System.err(642): 在 libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239) 11-07 15:18:24.945:W / System.err(642):在 libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273) 11-07 15:18:24.945: W/System.err(642): 在 libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168) 11-07 15:18:24.945: W/System.err(642): 在 libcore.net.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:271) 11-07 15:18:24.955: W/System.err(642): 在 java.net.URL.openStream(URL.java:462) 11-07 15:18:24.955: W / System.err(642):在 de.blender4me.einkaufsliste.internetHandler.getList(internetHandler.java:21) 11-07 15:18:24.955: W/System.err(642): 在 de.blender4me.einkaufsliste.MainActivity.populateListView(MainActivity.java:100) 11-07 15:18:24.955: W/System.err(642): 在 de.blender4me.einkaufsliste.MainActivity.onCreate(MainActivity.java:37) 11-07 15:18:24.955: W/System.err(642): 在 android.app.Activity.performCreate(Activity.java:5008) 11-07 15:18:24.955:W / System.err(642):在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 11-07 15:18:24.955: W/System.err(642): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023) 11-07 15:18:24.965: W/System.err(642): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 11-07 15:18:24.965: W/System.err(642): 在 android.app.ActivityThread.access$600(ActivityThread.java:130) 11-07 15:18:24.965:W / System.err(642):在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 11-07 15:18:24.965: W/System.err(642): 在 android.os.Handler.dispatchMessage(Handler.java:99) 11-07 15:18:24.965:W / System.err(642):在 android.os.Looper.loop(Looper.java:137) 11-07 15:18:24.965: W / System.err(642):在 android.app.ActivityThread.main(ActivityThread.java:4745) 11-07 15:18:24.965:W / System.err(642):在 java.lang.reflect.Method.invokeNative(Native Method) 11-07 15:18:24.965:W / System.err(642):在 java.lang.reflect.Method.invoke(Method.java:511) 11-07 15:18:24.975: W / System.err(642):在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 11-07 15:18:24.975: W/System.err(642): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 11-07 15:18:24.975:W / System.err(642):在 dalvik.system.NativeStart.main(本机方法)11-07 15:18:26.455: D/gralloc_goldfish(642):没有检测到 GPU 仿真的仿真器。
【问题讨论】:
-
你的日志猫说什么?
-
@mithrop 我编辑了我的帖子
标签: java android sdk bufferedreader