public class MainActivity extends Activity {
TableLayout tl;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tl= (TableLayout) findViewById(R.id.table);
class DownloadTask extends AsyncTask<String, Void, String>
{
String result = null;
InputStream is = null;
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.1.3/worker2.php");
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.e("log_tag", "connection success");
} catch (Exception e) {
Log.e("log_tag", "Error in http connection" + e.toString());
}
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
try {
JSONArray jArray = new JSONArray(result);
TableLayout tv = (TableLayout) findViewById(R.id.table);
tv.removeAllViewsInLayout();
int flag = 1;
for (int i = -1; i < jArray.length() - 1; i++) {
TableRow tr = new TableRow(MainActivity.this);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
if (flag == 1) {
TextView b6 = new TextView(MainActivity.this);
b6.setText("Id");
b6.setTextColor(Color.BLUE);
b6.setTextSize(15);
tr.addView(b6);
TextView b19 = new TextView(MainActivity.this);
b19.setPadding(10, 0, 0, 0);
b19.setTextSize(15);
b19.setText("Name");
b19.setTextColor(Color.BLUE);
tr.addView(b19);
TextView b29 = new TextView(MainActivity.this);
b29.setPadding(10, 0, 0, 0);
b29.setText("Status");
b29.setTextColor(Color.BLUE);
b29.setTextSize(15);
tr.addView(b29);
tv.addView(tr);
final View vline = new View(MainActivity.this);
vline.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 2));
vline.setBackgroundColor(Color.BLUE);
tv.addView(vline);
flag = 0;
}
else
{
JSONObject json_data = jArray.getJSONObject(i);
Log.i("log_tag", "id: " + json_data.getInt("Worker_id") + ", Username: " + json_data.getString("Worker_name") + ", No: " + json_data.getString("Nationality"));
TextView b = new TextView(MainActivity.this);
String stime = String.valueOf(json_data.getInt("Worker_id"));
b.setText(stime);
b.setTextColor(Color.RED);
b.setTextSize(15);
tr.addView(b);
TextView b1 = new TextView(MainActivity.this);
b1.setPadding(10, 0, 0, 0);
b1.setTextSize(15);
String stime1 = json_data.getString("Worker_name");
b1.setText(stime1);
b1.setTextColor(Color.BLACK);
tr.addView(b1);
TextView b2 = new TextView(MainActivity.this);
b2.setPadding(10, 0, 0, 0);
String stime2 = json_data.getString("Nationality");
b2.setText(stime2);
b2.setTextColor(Color.BLACK);
b2.setTextSize(15);
tr.addView(b2);
tv.addView(tr);
final View vline1 = new View(MainActivity.this);
vline1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
vline1.setBackgroundColor(Color.WHITE);
tv.addView(vline1);
}
}
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data" + e.toString());
Toast.makeText(getApplicationContext(), "JsonArray fail", Toast.LENGTH_SHORT).show();
}
super.onPostExecute(result);
}
}
DownloadTask dl = new DownloadTask();
dl.execute();
}
错误
10-13 15:21:20.743: E/log_tag(3135): Error in http connectionorg.apache.http.conn.HttpHostConnectException: Connection to http://localhost:3307 refused
10-13 15:21:20.763: E/AndroidRuntime(3135): FATAL EXCEPTION: main
10-13 15:21:20.763: E/AndroidRuntime(3135): Process: com.example.inform, PID: 3135
10-13 15:21:20.763: E/AndroidRuntime(3135): java.lang.NullPointerException
10-13 15:21:20.763: E/AndroidRuntime(3135): at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
10-13 15:21:20.763: E/AndroidRuntime(3135): at org.json.JSONTokener.nextValue(JSONTokener.java:94)
10-13 15:21:20.763: E/AndroidRuntime(3135): at org.json.JSONArray.<init>(JSONArray.java:92)
10-13 15:21:20.763: E/AndroidRuntime(3135): at org.json.JSONArray.<init>(JSONArray.java:108)
10-13 15:21:20.763: E/AndroidRuntime(3135): at com.example.inform.MainActivity$1DownloadTask.onPostExecute(MainActivity.java:69)
10-13 15:21:20.763: E/AndroidRuntime(3135): at com.example.inform.MainActivity$1DownloadTask.onPostExecute(MainActivity.java:1)
10-13 15:21:20.763: E/AndroidRuntime(3135): at android.os.AsyncTask.finish(AsyncTask.java:632)
10-13 15:21:20.763: E/AndroidRuntime(3135): at android.os.AsyncTask.access$600(AsyncTask.java:177)
10-13 15:21:20.763: E/AndroidRuntime(3135): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
10-13 15:21:20.763: E/AndroidRuntime(3135): at android.os.Handler.dispatchMessage(Handler.java:102)
10-13 15:21:20.763: E/AndroidRuntime(3135): at android.os.Looper.loop(Looper.java:136)
10-13 15:21:20.763: E/AndroidRuntime(3135): at android.app.ActivityThread.main(ActivityThread.java:5001)
10-13 15:21:20.763: E/AndroidRuntime(3135): at java.lang.reflect.Method.invokeNative(Native Method)
10-13 15:21:20.763: E/AndroidRuntime(3135): at java.lang.reflect.Method.invoke(Method.java:515)
10-13 15:21:20.763: E/AndroidRuntime(3135): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
10-13 15:21:20.763: E/AndroidRuntime(3135): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
10-13 15:21:20.763: E/AndroidRuntime(3135): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
问题是,如果我尝试调用该链接时Url无法正常工作,则会出现连接超时错误。
问题可能是网络(内部网络)中的给定网址范围或其他内容请查看您的服务器配置是否可以在全局访问网址
和 对于调用API,HttpClient不好,这是我的建议,来自api级别Marshmallow(23)HttpClient不支持所以更好地迁移到HttpUrlConnection 请按照链接中的代码