0
我的网络连接和下载数据有问题;你能帮我理解我的错误吗?我张贴代码:在Android中连接和下载数据
public class MainActivity extends Activity implements OnClickListener{
private static final String DEBUG_TAG = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final String DEBUG_TAG = "HttpExample";
//Assegno le View
Button bottone = (Button) findViewById(R.id.button);
TextView insert = (TextView) findViewById(R.id.inserisci);
bottone.setOnClickListener(this);
}
@Override
public void onClick(View v) {
TextView data = (TextView)findViewById(R.id.content);
EditText edit = (EditText)findViewById(R.id.edit);
//Assegna alla stringa sito il testo preso dall'EditText
String sito = edit.getText().toString();
try {
//Setta nella TextView i 500 caratteri del contenuto della web page
data.setText(downloadUrl(sito));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/* Dato un URL, stabilisce un HttpUrlConnection e restituisce il contenuto della web page
come un InputStream, che è ritornato come stringa.*/
public String downloadUrl(String myurl) throws IOException {
InputStream is = null;
//Mostra solo i primi 500 caratteri del contenuto della web page.
int len = 500;
try {
URL url = new URL(myurl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000 /* millisecondi */);
conn.setConnectTimeout(15000 /* millisecondi */);
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.setDoOutput(true);
// Inizializza la connessione
conn.connect();
int response = conn.getResponseCode();
Log.d(DEBUG_TAG, "The response is: " + response);
is = conn.getInputStream();
// Converte l'InputStream in una stringa
String contentAsString = readIt(is, len);
return contentAsString;
// Si assicura che l'InputStream sia chiuso dopo che l'app ha finito di usarlo
} finally {
if (is != null) {
is.close();
}
}
}
// Legge un InputStream e lo converte in una stringa
public String readIt(InputStream stream, int len) throws IOException, UnsupportedEncodingException {
Reader reader = null;
reader = new InputStreamReader(stream, "UTF-8");
char[] buffer = new char[len];
reader.read(buffer);
return new String(buffer);
}
}
文件XML是:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/inserisci"
android:text="@string/inserisci"
android:layout_width="wrap_content"/>
<EditText
android:id="@+id/edit"
android:layout_width="match_parent"/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button"
android:text="@string/button"
android:layout_width="wrap_content"
android:layout_marginLeft="80dp"
android:layout_gravity="center"/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center">
<TextView
android:text=""
android:id="@+id/content"/>
</TableRow>
</TableLayout>
你得到的错误是什么?您可以发布堆栈跟踪还是描述问题? – tristan2468 2014-10-17 14:56:01
嗨,谢谢你的回答!该应用程序崩溃时,必须从方法“downloadUrl”返回字符串;我还没有理解这个问题。我复制API的方法downloadUrl: – Francesco 2014-10-17 15:15:46
我敢打赌,你会得到一个异常,说你不能在主UI线程上执行http。如果是这种情况,您需要阅读后台线程主题。 Android开发人员培训网站有一个不错的[教程](http://developer.android.com/training/basics/network-ops/connecting.html) – 2014-10-17 15:17:00