java - Android:MySQL 和 JSON 如此接近?

标签 java php android mysql json

我已经完成了所有设置,但我遇到了与我读过的许多用户相同的问题,其中网址仅显示在应用程序屏幕上。我的 LogCat 内容如下:

http 连接 java.net.SocketException 时出错:权限被拒绝

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

JSONTester.java:

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 android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.LinearLayout;
import android.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:android="http://schemas.android.com/apk/res/android"
  package="com.uDrew.JSONTester"
  android:versionCode="1"
  android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".JSONTester"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

</application>

<uses-permission android:name="android.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

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

请告诉我这可能是什么。

最佳答案

其实我也遇到过同样的问题。当我尝试在手机上运行客户端时,出现此错误“http 连接 java.net.SocketException 中的错误:权限被拒绝”。问题是,我的 WiFi 已关闭。也许您的情况也是如此。

关于java - Android:MySQL 和 JSON 如此接近?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6127659/

相关文章:

java - 无法在 Windows 上的 bash 上访问 jarfile

php - 提供从 Python 脚本到 PHP 的反馈

Android:使用jackson逐对象解析JSON对象

android - 不兼容的 gradle 依赖项

c# - 如何在 xamarin android 项目上集成 webrtc android native 库?

java - 不同时区服务器上的 Hibernate 日期问题

java - 优秀的多线程 Java 代码示例?

php - 有人能帮我理解为什么这个 MySQL 更新代码不工作 php 5

php - 捕获用户上次登录日期

java - 如何解决 Java 和 Spring Boot 中的 slf4j logback 类路径错误?