我需要你的帮助。我获得了Internet许可,但是当我尝试从此网址获取数据时,它无法访问我的textview
。当我按下按钮时,总是说“sunucucevabı”,我想在我的textview
中看到这个Json数组,然后ı想要稍后解析它们。
这是我的代码
public class MainActivity extends AppCompatActivity {
TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button =(Button)findViewById(R.id.btn);
text =(TextView)findViewById(R.id.textView);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
text.setText("Sunucu cevabı");
new arkaPlan().execute("https://restcountries.eu/rest/v2/capital/tallinn");
//bağlanmasını istediğimiz yer
}
});
}
class arkaPlan extends AsyncTask<String,String,String> {
protected String doInBackground(String... params) {
// ilk elamanı sunucu adresi
HttpURLConnection connection = null;
BufferedReader br = null;
try {
URL url = new URL(params[0]); //http://
connection = (HttpURLConnection) url.openConnection();
connection.connect(); // sunucuya bağlandık
InputStream is = connection.getInputStream();
br = new BufferedReader(new InputStreamReader(is));
String satir;
String dosya = "";
while ((satir = br.readLine()) != null) {
Log.d("satir ", satir);
dosya += satir;
}
return dosya;
} catch (Exception e) {
e.printStackTrace();
}
return "hata";
}
@Override
protected void onPostExecute(String s) { // dönen bilgi burdaki String s içinde olur.
Log.d("postExecute",s);
text.setText(s);
}
}
}
答案 0 :(得分:0)
要在完成任务后更新UI,请执行onProgressUpdate()
。
@Override
protected void onProgressUpdate(String... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
((Activity) context).runOnUiThread(new Runnable() {
public void run() {
TextView text =(TextView)findViewById(R.id.textView);
text.setText(values.toString());
}
});
}
答案 1 :(得分:0)
****I solved it****
package deneme.json.jsondeneme;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import org.w3c.dom.Text;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
TextView text;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
text= (TextView) findViewById(R.id.textView);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new arkaPlan().execute();
}
});
}
protected class arkaPlan extends AsyncTask<Void,Void,Void>{
BufferedReader br = null;
StringBuilder sb = new StringBuilder();
String line,result= "";
@Override
protected Void doInBackground(Void... voids) {
try {
URL url = new URL("https://restcountries.eu/rest/v2/capital/tallinn");
br = new BufferedReader(new InputStreamReader(url.openStream()));
while((line = br.readLine())!= null){
sb.append(line).append("\n");
}
result = sb.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
text.setText(result);
super.onPostExecute(aVoid);
}
}
}