我无法通过PHP获取信息来调试我的程序。它在一个分离的apk中工作但是当我用一个按钮设置它时,这是我总是得到的错误。
“ServiceHandler”,“无法从网址获取任何数据”
所以这是我的代码使用JSON
package net.example.ab;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class AbJasonActivity extends Activity {
private ProgressDialog pDialog;
// URL to get contacts JSON
private static String url = "http://192.168.2.9/android_connect/ivpcms.php";
// JSON Node names
private static final String TAG_LISTS= "records_tbl";
private static final String TAG_RECORDS = "rec_id";
private static final String TAG_PHYSICIAN = "phy_id";
private static final String TAG_PATIENT = "pat_id";
private static final String TAG_VOLUME = "vol_id";
private static final String TAG_DROP = "dro_id";
private static final String TAG_MINUTES = "rec_minutes";
private static final String TAG_DPM = "dpm";
TextView test;
// contacts JSONArray
JSONArray lists = null;
// Hashmap for ListView
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lol);
test = (TextView) findViewById(R.id.test);
test.setText(""+ lists);
// Calling async task to get json
new GetDoctors().execute();
}
/**
* Async task class to get json by making HTTP call
* */
private class GetDoctors extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(AbJasonActivity.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
lists = jsonObj.getJSONArray(TAG_LISTS);
// looping through All Contacts
JSONObject c = lists.getJSONObject(0);
String rec_id = c.getString(TAG_RECORDS);
String phy_id = c.getString(TAG_PHYSICIAN);
String pat_id = c.getString(TAG_PATIENT);
String vol_id = c.getString(TAG_VOLUME);
String dro_id = c.getString(TAG_DROP);
String rec_minutes = c.getString(TAG_MINUTES);
double dpm = c.getDouble(TAG_DPM);
String dpm2 = String.valueOf(dpm);
// tmp hashmap for single contact
HashMap<String, String> doctor = new HashMap<String, String>();
// adding each child node to HashMap key => value
doctor.put(TAG_RECORDS, rec_id);
doctor.put(TAG_PHYSICIAN, phy_id);
doctor.put(TAG_PATIENT, pat_id);
doctor.put(TAG_VOLUME, vol_id);
doctor.put(TAG_DROP, dro_id);
doctor.put(TAG_MINUTES, rec_minutes);
doctor.put(TAG_DPM, dpm2);
// adding contact to contact list
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */
}
}
}
我的xml代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<!-- Main ListView
Always give id value as list(@android:id/list)
-->
<TextView
android:id="@+id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/testing" />
</LinearLayout>
我的按钮代码正常工作
private void setupMessageButtonAb() {
// 1. for the button
Button messageButton = (Button) findViewById(R.id.button_ab);
//2. set click listener
messageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i(AB, "You've chosen Dr. Melegrito!");
Toast.makeText(SecondActivity.this,
"You've chosen Dr. Melegrito",
Toast.LENGTH_SHORT)
.show();
startActivity(new Intent(SecondActivity.this, AbJasonActivity.class));
}
});
}`
PS :因为我的JSON代码正在运行另一个apk并且网络服务器正在运行,并且正确添加了IP。我不知道我的错误是什么。感谢您的帮助:)