我已经在其他活动中完成了这个过程并且有效!
我不知道为什么我一直在json上得到NullPointerException,我看不出代码有什么问题,如果有人可以帮助我,我会非常感激!
package com.mobimapa;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class UserPage extends ListActivity {
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> processosList;
// url to get all products list
private static final String url_all_products = "http://10.0.2.2/mobimapa/GetProcessos.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_PROCESSOS = "processo";
private static final String TAG_PID = "id_processo";
private static final String TAG_NAME = "nome_processo";
private static final String TAG_DATA = "data";
private static final String TAG_UID = "id_user";
private static final String TAG_USERNAME = "username";
String username1;
// products JSONArray
JSONArray processos = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.userpage);
// getting product details from intent
Intent i = getIntent();
// getting product id (pid) from intent
username1 = i.getStringExtra("username");
Log.d("Username", username1);
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText(getIntent().getExtras().getString("username"));
// Hashmap for ListView
processosList = new ArrayList<HashMap<String, String>>();
// Loading products in Background Thread
new LoadAllProducts().execute();
}
/**
* Background Async Task to Load all product by making HTTP Request
* */
class LoadAllProducts extends AsyncTask<String, String, String> {
/**
* getting All products from url
* */
protected String doInBackground(String... args) {
runOnUiThread(new Runnable() {
public void run() {
// Check for success tag
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", username1));
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);
// Check your log cat for JSON reponse
Log.d("All Products: ", json.toString());
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Products
processos = json.getJSONArray(TAG_PROCESSOS);
// looping through All Products
for (int i = 0; i < processos.length(); i++) {
JSONObject c = processos.getJSONObject(i);
// Storing each json item in variable
String username = c.getString(TAG_USERNAME);
//String name = c.getString(TAG_NAME);
//String data = c.getString(TAG_DATA);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_USERNAME, username);
//map.put(TAG_NAME, name);
//map.put(TAG_DATA, data);
// adding HashList to ArrayList
processosList.add(map);
ListAdapter adapter = new SimpleAdapter(
UserPage.this, processosList,
R.layout.list_tem, new String[] { TAG_USERNAME
},
new int[] { R.id.name });
// updating listview
setListAdapter(adapter);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
return null;
}
}
}
PHP代码,这个php代码只是为了看看是否收到变量。
<?php
// array for JSON response
$response = array();
mysql_connect("localhost","root",""); // host, username, password...
mysql_select_db("mobimapa"); // db name...
$username = $_GET['username'];
// get all products from products table
$result = mysql_query("SELECT username FROM user WHERE username = $username");
// check for empty result
if (mysql_num_rows($result) > 0) {
$response["processo"] = array();
// check for empty result
while ($row = mysql_fetch_array($result)) {
$processo = array();
$processo["username"] = $row["username"];
// success
$response["success"] = 1;
// user node
$response["processo"] = array();
array_push($response["processo"], $processo);
}
// echoing JSON response
echo json_encode($processo);
}
?>
堆栈跟踪
**08-18 15:56:39.224: D/Username(1754): Maria
08-18 15:56:40.394: E/JSON Parser(1754): Error parsing data org.json.JSONException: Value br of type java.lang.String cannot be converted to JSONObject
08-18 15:56:40.554: E/AndroidRuntime(1754): FATAL EXCEPTION: main
08-18 15:56:40.554: E/AndroidRuntime(1754): java.lang.NullPointerException
08-18 15:56:40.554: E/AndroidRuntime(1754): at com.mobimapa.UserPage$LoadAllProducts$1.run(UserPage.java:196)**
08-18 15:56:40.554: E/AndroidRuntime(1754): at android.os.Handler.handleCallback(Handler.java:725)
08-18 15:56:40.554: E/AndroidRuntime(1754): at android.os.Handler.dispatchMessage(Handler.java:92)
08-18 15:56:40.554: E/AndroidRuntime(1754): at android.os.Looper.loop(Looper.java:137)
08-18 15:56:40.554: E/AndroidRuntime(1754): at android.app.ActivityThread.main(ActivityThread.java:5041)
08-18 15:56:40.554: E/AndroidRuntime(1754): at java.lang.reflect.Method.invokeNative(Native Method)
08-18 15:56:40.554: E/AndroidRuntime(1754): at java.lang.reflect.Method.invoke(Method.java:511)
08-18 15:56:40.554: E/AndroidRuntime(1754): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-18 15:56:40.554: E/AndroidRuntime(1754): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-18 15:56:40.554: E/AndroidRuntime(1754): at dalvik.system.NativeStart.main(Native Method)