我正在尝试将sharedPreferences
的值发布到我的PHP文件中。这是活动的代码:
public class ViewGradesActivity extends ListActivity {
private ProgressDialog pDialog;
// URL to get contacts JSON
private static String url = "http://usamobileapp.pe.hu/webservice/viewGradesJSON.php";
// JSON Node name
private static final String TAG_GRADES = "grades";
private static final String TAG_SUBJECT = "subject";
private static final String TAG_PRELIM = "prelim";
private static final String TAG_MIDTERM = "midterm";
private static final String TAG_FINALS = "final";
private static final String TAG_AVERAGE = "average";
private static final String TAG_REMARKS = "remarks";
HttpPost httppost;
StringBuffer buffer;
HttpResponse response;
HttpClient httpclient;
List<NameValuePair> nameValuePairs;
ProgressDialog dialog = null;
// contacts JSONArray
JSONArray grades = null;
// Hashmap for ListView
ArrayList<HashMap<String, String>> gradesList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_grades);
gradesList = new ArrayList<HashMap<String, String>>();
ListView lv = getListView();
// Calling async task to get json
new GetContacts().execute();
}
/**
* Async task class to get json by making HTTP call
* */
private class GetContacts extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(ViewGradesActivity.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
String awesomeString = PreferenceManager.getDefaultSharedPreferences(ViewGradesActivity.this).getString("key", "");
httpclient=new DefaultHttpClient();
httppost= new HttpPost("http://usamobileapp.pe.hu/webservice/viewGradesJSON.php");
nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("username",awesomeString));
try {
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
try {
response=httpclient.execute(httppost);
} catch (IOException e) {
e.printStackTrace();
}
// Creating service handler class instance
viewGrades_ServiceHandler sh = new viewGrades_ServiceHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url, classSched_ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
grades = jsonObj.getJSONArray(TAG_GRADES);
// looping through All Contacts
for (int i = 0; i < grades.length(); i++) {
JSONObject c = grades.getJSONObject(i);
String prelim = c.getString(TAG_PRELIM);
String subject = c.getString(TAG_SUBJECT);
String midterm = c.getString(TAG_MIDTERM);
String finals = c.getString(TAG_FINALS);
String ave = c.getString(TAG_AVERAGE);
String rem = c.getString(TAG_REMARKS);
// tmp hashmap for single contact
HashMap<String, String> gradeDetails = new HashMap<String, String>();
// adding each child node to HashMap key => value
gradeDetails.put(TAG_SUBJECT, subject);
gradeDetails.put(TAG_PRELIM, prelim);
gradeDetails.put(TAG_MIDTERM, midterm);
gradeDetails.put(TAG_FINALS, finals);
gradeDetails.put(TAG_AVERAGE, ave);
gradeDetails.put(TAG_REMARKS, rem);
// adding contact to contact list
gradesList.add(gradeDetails);
}
} 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
* */
ListAdapter adapter = new SimpleAdapter(
ViewGradesActivity.this, gradesList,
R.layout.viewgrades_listitem, new String[] { TAG_SUBJECT, TAG_PRELIM,
TAG_MIDTERM, TAG_FINALS, TAG_AVERAGE, TAG_REMARKS }, new int[] { R.id.name,
R.id.email, R.id.mobile, R.id.finals, R.id.average, R.id.remarks });
setListAdapter(adapter);
}
}
}
然后这是我使用$_POST
$username= $_POST['username'];
$query_search = "select * from grades where rfid = '".$username."'";
$result = mysql_query("select * from grades where rfid = '".$username."'", $conn);
我想知道为什么每当我尝试运行该应用程序时,当我进入此活动时它都不会显示任何内容。当我检查Logcat时,会出现上面提到的错误。
感谢您的帮助。
编辑:这是我在logcat中找到的完整日志:
03-23 22:07:54.009 23313-23354/com.example.kreshiathea.myfirstapp W/System.err﹕ org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
03-23 22:07:54.015 23313-23354/com.example.kreshiathea.myfirstapp W/System.err﹕ at org.json.JSON.typeMismatch(JSON.java:111)
03-23 22:07:54.016 23313-23354/com.example.kreshiathea.myfirstapp W/System.err﹕ at org.json.JSONObject.<init>(JSONObject.java:158)
03-23 22:07:54.016 23313-23354/com.example.kreshiathea.myfirstapp W/System.err﹕ at org.json.JSONObject.<init>(JSONObject.java:171)
03-23 22:07:54.017 23313-23354/com.example.kreshiathea.myfirstapp W/System.err﹕ at com.example.kreshiathea.myfirstapp.ViewGradesActivity$GetContacts.doInBackground(ViewGradesActivity.java:147)
03-23 22:07:54.018 23313-23354/com.example.kreshiathea.myfirstapp W/System.err﹕ at com.example.kreshiathea.myfirstapp.ViewGradesActivity$GetContacts.doInBackground(ViewGradesActivity.java:101)
03-23 22:07:54.019 23313-23354/com.example.kreshiathea.myfirstapp W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:287)
03-23 22:07:54.020 23313-23354/com.example.kreshiathea.myfirstapp W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:234)
03-23 22:07:54.021 23313-23354/com.example.kreshiathea.myfirstapp W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
03-23 22:07:54.022 23313-23354/com.example.kreshiathea.myfirstapp W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
03-23 22:07:54.023 23313-23354/com.example.kreshiathea.myfirstapp W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
03-23 22:07:54.024 23313-23354/com.example.kreshiathea.myfirstapp W/System.err﹕ at java.lang.Thread.run(Thread.java:838)