首页上一页 1 下一页尾页 1 条记录 1/1页
老师,为什么我的线程里面的Runnable里面的run方法进不去,以下是我的代码,麻烦大神帮忙看一下,谢谢!
发表在Android图书答疑
2020-04-26 悬赏:2 学分
《零基础学Android》第15章 网络编程及Internet应用 391页-403页
是否精华
是
否
版块置顶:
是
否
com.example.administrator.myapplication; android.app.Activity; android.os.Bundle; android.os.Handler; android.os.Message; ; android.view.View; android.widget.Button; android.widget.TextView; org.json.JSONException; org.json.JSONObject; java.io.BufferedReader; java.io.IOException; java.io.InputStreamReader; java.net.HttpURLConnection; java.net.MalformedURLException; android.app.Activity; android.os.Bundle; android.os.Handler; android.os.Message; ; android.support.v7.app.AppCompatActivity; android.view.View; android.widget.Button; android.widget.TextView; org.json.JSONException; org.json.JSONObject; java.io.BufferedReader; java.io.IOException; java.io.InputStreamReader; java.net.HttpURLConnection; java.net.MalformedURLException; java.net.URL; MainActivity Activity { TextView ; TextView ; TextView ; TextView ; Handler ; Button ; onCreate(Bundle savedInstanceState) { .onCreate(savedInstanceState); setContentView(R.layout.); = (TextView) findViewById(R.id.); = (TextView) findViewById(R.id.); = (TextView) findViewById(R.id.); = (TextView) findViewById(R.id.); = (Button) findViewById(R.id.); = Handler(){ handleMessage(Message msg) { String result = (String)msg.; JSONObject jsonObject = ; { jsonObject = JSONObject(result); .setText(jsonObject.getString()); .setText(jsonObject.getString()); .setText(jsonObject.getString()); .setText(jsonObject.getString()); } (JSONException e) { e.printStackTrace(); } .handleMessage(msg); } }; .setOnClickListener(View.OnClickListener() { onClick(View v) { Thread(Runnable() { run() { String result = getDataFromTomcat(); Message msg = .obtainMessage(); msg.= result; .sendMessage(msg); } }).start(); } }); } String getDataFromTomcat(){ String target = ; URL url; String result=; { url = URL(target); HttpURLConnection urlConn = (HttpURLConnection) url.openConnection(); urlConn.setDoOutput(); urlConn.setRequestMethod(); urlConn.setUseCaches(); urlConn.setInstanceFollowRedirects(); InputStreamReader in = InputStreamReader(urlConn.getInputStream()); BufferedReader buffer = BufferedReader(in); String inputLine=; ((inputLine = buffer.readLine())!=){ result += inputLine; } in.close(); urlConn.disconnect(); } (MalformedURLException e) { e.printStackTrace(); } (IOException e) { e.printStackTrace(); } result; } }