我正在尝试使用JSON和HTTP客户端从我的数据库中的表中检索数据。我的代码似乎正在工作,但它没有输出任何东西,但如果我在查询中故意更改错误吐司输出,所以我知道代码是正确的没有错误输出。问题在于弄清楚如何将JSON输出放到我的视图屏幕上。
以下是完整的HTTP代码:
public void LoadRemote()
{
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://deucalion0.co.uk/getscores.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection"+e.toString());
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
String line="0";
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
//paring data
int score;
String username;
try{
jArray = new JSONArray(result);
JSONObject json_data=null;
for(int i=0;i<jArray.length();i++){
json_data = jArray.getJSONObject(i);
score=json_data.getInt("score");
username=json_data.getString("username");
}
}
catch(JSONException e1){
Toast.makeText(getBaseContext(), "No City Found" ,Toast.LENGTH_LONG).show();
} catch (ParseException e1) {
e1.printStackTrace();
}
}
这是我的SQL查询:
$sql=mysql_query("select score, username from highscores ");
while($row=mysql_fetch_assoc($sql))
$output[]=$row;
print(json_encode($output));
您可以看到此工作here
我很感激有关这个问题的任何建议。
感谢。
由于并发症,我已经重新开始,这里是活动类,现在是:
public class Viewscores extends Activity{
Button scores;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewscores);
scores = (Button) findViewById(R.id.bViewscores);
scores.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
LoadRemote();
}
});
}
public void LoadRemote()
{
String result = "";
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
InputStream is = null;
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://deucalion0.co.uk/getscores.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection"+e.toString());
}
BufferedReader myReader = new BufferedReader(new InputStreamReader(is));
}
}
答案 0 :(得分:1)
我可能错了,但是如果你创建一个像这样的包装器对象并不容易:
public class Response {
public int score;
public String username;
}
然后用gson反序列化json很容易:
ArrayList<Response> responses = new ArrayList<Response>();
Gson gson = new Gson();
Type collectionType = new TypeToken<List<Response>>(){}.getType();
responses = gson.fromJson(response, collectionType);
其中response是HTTP请求中的String。有关gson的更多信息: http://code.google.com/p/google-gson/
答案 1 :(得分:1)
我无法完全理解你的意思是“将JSON输出放到我的视图屏幕上”..因为你没有提到你正在使用的视图..所以假设你使用listview这个......这就是什么我认为可以做到。
try{
jArray = new JSONArray(result);
int k=jArray.length();
String s[]=new String[k];
for(int u=0;u<k;u++){
json_data = jArray.getJSONObject(i);
s[u]=json_data.getInt("score")+" "+json_data.getString("username");
}
setContentView(R.layout.fine);
ListView lv = (ListView)findViewById(R.id.list);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.list,R.id.textView1,s);
lv.setAdapter(adapter);
答案 2 :(得分:1)
好的..这是整个代码...
public class Viewscores extends Activity{
Button scores;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewscores);
scores = (Button) findViewById(R.id.bViewscores);
scores.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
Intent i=new Intent(this,lst.class);
startActivity(i);
}
});
}
这里是“lst”类
public class lst extends ListActivity{
JSONArray jArray;
String result = null;
InputStream is = null;
StringBuilder sb=null;
String s[];
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try{
URL u=new URL("http://deucalion0.co.uk/getscores.php");
URLConnection tc=u.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(tc.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
jArray =new JSONArray(line);
s=new String[jArray.length()];
for(int m=0;m<jArray.length();m++){
JSONObject json_data=(JSONObject)jArray.get(m);
s[m]=json_data.getInt("score")+" "+json_data.getString("username");
}
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, s);
setListAdapter(adapter);
}
catch(JSONException e1){
} catch (ParseException e1) {
e1.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} |
} ---
这个肯定有用......试试这个......:)