所以我正在尝试从URL读取流并将结果设置为文本视图。 问题是Android Studio的代码未在我的设备上返回任何结果,而Net Bean上的相同代码返回了流输出。我不明白问题出在哪里,因为我使用的是与Android Studio和Netbeans相同的代码。Alsandroid studio上的应用程序没有崩溃或引发任何错误。
只是让您知道我已经在手机的浏览器中打开了android studio代码中的URL,并且该URL正在运行。我也在清单中添加了互联网许可。我确实将功能changeText放在了“ onClick”属性中。
以下代码已在NetBeans和Android Studio(在我的银河S6上)上进行了测试。
Android Studio代码:
package com.example.mohammed.acc;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
public void changeText (View v){
TextView t1 = (TextView)findViewById(R.id.t1);
URL url;
HttpURLConnection con;
try{
StringBuilder sb = new StringBuilder();
url = new URL("http://192.168.1.104:8000/api");
con = (HttpURLConnection) url.openConnection();
BufferedInputStream in = new BufferedInputStream(con.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String Line = "";
while((Line = reader.readLine()) != null){
sb.append(Line);
}
t1.append(sb.toString());
in.close();
}
catch (MalformedURLException ex) {
t1.setText(ex.getMessage());
}
catch (IOException ex){
t1.setText(ex.getMessage());
} catch (Exception ex){
t1.setText(ex.getMessage());
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
NetBeans代码:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication21;
import java.io.BufferedInputStream;
import java.util.Stack;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class JavaApplication21 {
public static void main(String[] args) {
try{
StringBuilder sb = new StringBuilder();
URL url = new URL("http://127.0.0.1:8000/api");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
BufferedInputStream in = new BufferedInputStream(con.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String Line = "";
while((Line = reader.readLine()) != null){
sb.append(Line);
}
System.out.print(sb.toString());
in.close();
}
catch (Exception ex){
System.out.print(ex.getMessage());
}
}
}
NetBeans输出: 如您所见,它正在工作。 Android Studio,我手机的输出: 记录快照。这是在运行并单击按钮之后。
答案 0 :(得分:1)
如果您的代码被调用,则应引起NetworkOnMainThreadException(您不能在主线程上进行网络调用)。因此很可能根本没有调用它,并且您的按钮处理代码已损坏。
实际上,我看到您正在捕获Exception。是的,它将引发NetworkOnMainThreadException,而您正在捕获并忽略它。如果您不愿意打印例外记录,甚至可以通过调试器中的基本操作或阅读日志来告诉您。如果您不在某个地方打印例外,如果您没有看到任何错误却什么也没发生,请不要感到惊讶。
也不是一个错误,但是在事件处理程序中运行findViewById被认为是不好的做法。这些应该在onCreate中运行一次并缓存结果。
答案 1 :(得分:0)
我看到2个问题:
1:您的http io不在工作线程中执行;
2:在运行时未请求许可(您的api级别可能高于22);
import android.annotation.TargetApi;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.example.root.myapplication.R;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
@TargetApi(23)
protected void askPermissions() {
String[] permissions = {
"android.permission.INTERNET"
};
int requestCode = 200;
requestPermissions(permissions, requestCode);
}
public void changeText (View v){
final TextView t1 = (TextView)findViewById(R.id.t1);
new AsyncTask<Void,Void,Void>(){
private Exception exception;
private StringBuilder sb;
@Override
protected Void doInBackground(Void... voids) {
try{
sb = new StringBuilder();
URL url;
HttpURLConnection con;
url= new URL("http://peyvandha.ir");
con= (HttpURLConnection) url.openConnection();
BufferedInputStream in = new BufferedInputStream(con.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String Line = "";
while((Line = reader.readLine()) != null){
sb.append(Line);
}
in.close();
}catch (Exception ex){
this.exception=ex;
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
if(sb!=null){
//do what you do;
t1.setText(sb.toString());
}else {
t1.setText(exception.getMessage());
}
super.onPostExecute(aVoid);
}
}.execute();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(Build.VERSION.SDK_INT>22)askPermissions();
}
}