有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Android:MySQL和JSON如此接近?

我做了所有的设置,我遇到了和我读到的很多用户一样的问题,url只显示在应用程序屏幕上。我的日志显示:

http连接java中出现错误。网SocketException:权限被拒绝

我的权限是按照清单中的建议设置的。我将应用程序设置为连接到运行php脚本的Web服务器,该脚本在导航到时似乎输出正确。我在模拟器中运行这个程序,并使用Eclipse Helios 1.3.2服务版本2。所有的建议都会有帮助

杰森特。爪哇:

package com.uDrew.JSONTester;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import 安卓.app.Activity;
import 安卓.os.Bundle;
import 安卓.util.Log;
import 安卓.widget.LinearLayout;
import 安卓.widget.TextView;


public class JSONTester extends Activity {
/** Called when the activity is first created. */

   TextView txt;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    // Create a crude view - this should really be set via the layout resources  
    // but since its an example saves declaring them in the XML.  
    LinearLayout rootLayout = new LinearLayout(getApplicationContext());  
    txt = new TextView(getApplicationContext());  
    rootLayout.addView(txt);  
    setContentView(rootLayout);  

    // Set the text and call the connect function.  
    txt.setText("Connecting..."); 
  //call the method to run the data retrieval
    txt.setText(getServerData(KEY_121)); 



}
public static final String KEY_121 = "http://www.myrealdomain.com/city.php"; 



private String getServerData(String returnString) {

   InputStream is = null;

   String result = "";
    //the year data to send
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("year","1970"));

    //http post
    try{
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(KEY_121);
            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);
            StringBuilder sb = new StringBuilder();
            String line = null;
            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());
    }
    //parse json data
    try{
            JSONArray jArray = new JSONArray(result);
            for(int i=0;i<jArray.length();i++){
                    JSONObject json_data = jArray.getJSONObject(i);
                    Log.i("log_tag","id: "+json_data.getInt("id")+
                            ", name: "+json_data.getString("name")+
                            ", sex: "+json_data.getInt("sex")+
                            ", birthyear: "+json_data.getInt("birthyear")
                    );
                    //Get an output to the screen
                    returnString += "\n\t" + jArray.getJSONObject(i); 
            }
    }catch(JSONException e){
            Log.e("log_tag", "Error parsing data "+e.toString());
    }
    return returnString; 
}    

}

AndroidManifest。xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
  package="com.uDrew.JSONTester"
  安卓:versionCode="1"
  安卓:versionName="1.0">
<application 安卓:icon="@drawable/icon" 安卓:label="@string/app_name">
    <activity 安卓:name=".JSONTester"
              安卓:label="@string/app_name">
        <intent-filter>
            <action 安卓:name="安卓.intent.action.MAIN" />
            <category 安卓:name="安卓.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

</application>

<uses-permission 安卓:name="安卓.permission.INTERNET" />
</manifest> 

城市。php:

<?php

      mysql_connect("www.myrealdomain.com:3306","root","mypassword");

      mysql_select_db("deal");

      $q=mysql_query("SELECT * FROM people WHERE birthyear>'".$_REQUEST['year']."'");

      while($e=mysql_fetch_assoc($q))

              $output[]=$e;

           print(json_encode($output));

    mysql_close();
?>

这个PHP文件在被导航到时会输出以下内容:[{"id":"1","name":"Andrew","sex":"1","birthyear":"1987"}]

PHP Notice: Undefined index: year in C:\Inetpub\city.php on line 7

这让我相信,除了没有从Android应用程序中传递变量之外,我的PHP没有什么问题,因为我只是在浏览器上导航到它

请告诉我这可能是什么


共 (1) 个答案

  1. # 1 楼答案

    要么修复PHP错误,要么键入

    
    error_reporting(0);
    
    

    您的JSON响应需要干净 但许可错误应该是另一个问题

    你能告诉我们更多LogCat的细节吗