您好我是android的新手,我跟着一些在线教程从android.But中的mysql获取数据当我试图获取数据时我得到json异常“没有价值:json数据”。
public class MainActivity extends ActionBarActivity {
String myJSON;
private static final String TAG_RESULTS="result";
private static final String TAG_NAME = "message_recd";
private static final String TAG_ADD ="message_sent";
JSONArray peoples = null;
ArrayList<HashMap<String, String>> personList;
ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.listView);
personList = new ArrayList<HashMap<String,String>>();
getData();
}
protected void showList(){
try {
JSONObject jsonObj = new JSONObject(myJSON);
peoples = jsonObj.getJSONArray(TAG_RESULTS);
for(int i=0;i<peoples.length();i++){
JSONObject c = peoples.getJSONObject(i);
String name=null, address=null;
if(c.has("message_recd"))
name = c.getString("message_recd");
else if(c.has("message_sent"))
address = c.getString("message_sent");
HashMap<String,String> persons = new HashMap<String,String>();
persons.put(TAG_NAME,name);
persons.put(TAG_ADD,address);
personList.add(persons);
}
ListAdapter adapter = new SimpleAdapter(
MainActivity.this, personList, R.layout.list_item,
new String[]{TAG_NAME,TAG_ADD},
new int[]{R.id.name, R.id.address}
);
list.setAdapter(adapter);
} catch (JSONException e) {
Log.i("tagconvertstr", "["+myJSON+"]");
}
}
public void getData(){
class GetDataJSON extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... params) {
DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
HttpPost httppost = new HttpPost("http://10.0.2.2/progress_card/messages/get_messages1.php");
// Depends on your web service
httppost.setHeader("Content-type", "application/json");
InputStream inputStream = null;
String result = null;
try {
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
inputStream = entity.getContent();
// json is UTF-8 by default
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
result = sb.toString();
} catch (Exception e) {
// Oops
}
finally {
try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
}
return result;
}
@Override
protected void onPostExecute(String result){
myJSON=result;
showList();
}
}
GetDataJSON g = new GetDataJSON();
g.execute();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
logcat的:
10-01 03:07:55.910: I/tagconvertstr(1953): [{"0":{"message_recd":"hello is it working really?"},"1":{"message_recd":"hello"},"2":{"message_recd":"checking"},"3":{"message_recd":"qwerty"},"4":{"message_recd":"qweqwdqdqd"},"5":{"message_recd":"ojhdsgwkjsbawdfvkjn"},"6":{"message_recd":"qwertyyt"},"7":{"message_recd":"qwertyytqwertyytqwertyytqwertyytqwertyytqwertyyt"},"8":{"message_sent":"hey whtas up"},"9":{"message_sent":"hey whtas up again"},"10":{"message_sent":"hey whtas up hey whtas up hey whtas up hey whtas up hey whtas up "}}
10-01 03:07:55.910: I/tagconvertstr(1953): ]
Json回复:
{"0":{"message_recd":"hello is it working really?"},"1":{"message_recd":"hello"},"2":{"message_recd":"checking"},"3":{"message_recd":"qwerty"},"4":{"message_recd":"qweqwdqdqd"},"5":{"message_recd":"ojhdsgwkjsbawdfvkjn"},"6":{"message_recd":"qwertyyt"},"7":{"message_sent":"hey whtas up"},"8":{"message_sent":"hey whtas up again"}}
答案 0 :(得分:0)
看到这是你想要做的: -
在帖子执行中,您可以: - myJSON=result;
现在myJson的json结构是: - {"0":{"message_recd":"hello is it working really?"},"1":{"message_recd":"hello"},"2":{"message_recd":"checking"},"3":{"message_recd":"qwerty"},"4":{"message_recd":"qweqwdqdqd"},"5":{"message_recd":"ojhdsgwkjsbawdfvkjn"},"6":{"message_recd":"qwertyyt"},"7":{"message_sent":"hey whtas up"},"8":{"message_sent":"hey whtas up again"}}
在Json {}中表示Json对象,而[]表示其Json Array ..现在在方法showList()中执行: -
JSONObject jsonObj = new JSONObject(myJSON);
peoples = jsonObj.getJSONArray(TAG_RESULTS);
其中TAG_RESULTS =“结果”
所以我问Json Object jsonObj给我一个Json数组,其键是“result”但是没有,请参阅Json结构。事实上,Json对象上没有数组,它有多个带有键0,1,2,3 ... 8的Json对象。
所以为了从jsonObj获取所有Json对象,我们将循环这样: -
for(int i=0;i < jsonObj.length;i++) {
JsonObject myJsonObject = jsonObj.getJsonObject(i);
//check if it has value for key and do something with value
if(myJsonObject.has("message_recd")) {
String meesageRecieved = myJsonObject.getString("message_recd")
//Add it to the hash map
}
// do same if we have more keys like message_recd in json
}
你从那个网址得到的Json结构不是你认为你得到的,它没有结果数组..快乐的android编码:)
答案 1 :(得分:0)
你得到的回应不是json结构。
中的外观/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,6}$|^$/
但在android json应该看起来像:
{"0":{"message_recd":"hello is it working really?"},"1":{"message_recd":"hello"},"2":{"message_recd":"checking"},"3":{"message_recd":"qwerty"},"4":{"message_recd":"qweqwdqdqd"},"5":{"message_recd":"ojhdsgwkjsbawdfvkjn"},"6":{"message_recd":"qwertyyt"},"7":{"message_sent":"hey whtas up"},"8":{"message_sent":"hey whtas up again"}}