2011-05-25 1 views
0

私はすべての設定を手に入れました。私はこれまでに読んだことのある多くのユーザーと同じ問題を抱えています。ここでは、URLはアプリの画面にのみ表示されます。私のLogCatは読む:http接続のjava.net.SocketExceptionがでAndroid:MySQLとJSONが近いですか?

エラー:許可が

を否定したマニフェストで提案されているように私の権限が設定されています。私は、アプリをナビゲートすると正しく出力されるように見えるPHPスクリプトを実行しているWebサーバーに接続するように設定しています。私はこれをエミュレータで実行し、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> 

city.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に間違いがないと信じています。

これが何であるか教えてください。

+0

Btwは、 "応答を文字列に変換する"ためにEntityUtils.toString(エンティティ)を使用できます。 Bloody *同じ機能を自分で実装するたびに、Utilsは私にポップアップを続けます。同じ不具合を繰り返さないでください - – Audrius

答えて

0

どちらのPHPエラーを修正するか、あなたのJSON応答が きれいにする必要がある。しかし、許可エラーが別の問題であるべき

 

error_reporting(0); 
 

を入力します。

LogCatの詳細を教えてください。

+0

JSONは値がなければエラーで応答しませんか?$ _ REQUEST ['year']はAndroidアプリケーションから渡されますか? – O1DMBFan

+0

今日アプリケーションを実行して詳細情報を取得してくださいLogCatの詳細については、私は完璧に走った。元の投稿ソースから1ビットも変更していない。 – O1DMBFan

1

実際に私は同じ問題に直面しました。電話でクライアントを実行しようとしたときに、このエラー「http接続のjava.net.SocketException:アクセスが拒否されました」が表示されます。問題は、私はWiFiをオフにしていたということでした。あなたの場合も同じかもしれません。

関連する問題