gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 14:49:24 24 4
gpt4 key购买 nike

我已经完成了所有设置,但我遇到了与我读过的许多用户相同的问题,其中网址仅显示在应用程序屏幕上。我的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com