0

私はこの問題で立ち往生しています。mysql データベースから json データを取得しようとしています。現在、AsyncTask を使用してほとんどの作業をバックグラウンドで実行し、異なる php スクリプトを使用して異なるクエリを実行し、異なるデータを返したいと考えています (これが、直接 URL を httpost に挿入していない理由です)。

基本的に今、データベースからデータを取得し、それを変数に渡し、その変数を TextView に表示したいと考えています。

コードを実行すると、TextView に「データがありません」と表示されるだけなので、何かが機能していません。私のphpファイルは正常に動作します!コードは次のとおりです。どんな助けでも大歓迎です:)

package com.example.test1;



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.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.net.ParseException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class clubpage extends Activity {

TextView test;
String clubphp = "http://10.0.2.2/corkgaa/Nemo.php";
String event = "No Data"; 
String name = "No Data"; 
String price = "No Data"; 


@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.clubpage);

    test = (TextView)findViewById(R.id.clubinfo);

    new Dbhandler().execute(clubphp);

    test.setText(name);     

}

public class Dbhandler extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... arg0) {
        // TODO Auto-generated method stub
        JSONArray jArray; 
        String result = "no response"; 
        InputStream is = null; 
        StringBuilder sb = null;

        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        //http post 
        try{ 
            HttpClient httpclient = new DefaultHttpClient(); 
            HttpPost httppost = new HttpPost("clubphp"); 
            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()); 
            }

        //parsing data

        try{ 
            jArray = new JSONArray(result); 
        JSONObject json_data=null; 

        for(int i=0;i<jArray.length();i++){ 
            json_data = jArray.getJSONObject(i); 
            name=json_data.getString("NAME"); 
            event=json_data.getString("EVENT");
            price=json_data.getString("PRICE");
            }
        }
        catch(JSONException e1){ 

            }
        catch (ParseException e1) { 
            e1.printStackTrace();
            }
        return name;
    }   

}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
}

}

丸太の猫:

03-04 15:25:35.139: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:35.149: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:35.239: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:35.259: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:35.259: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:35.270: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:35.279: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:35.279: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:35.312: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:35.312: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:35.349: I/ActivityManager(288): START u0 {cmp=com.example.test1/.clubpage} from pid 791
03-04 15:25:35.369: W/WindowManager(288): Failure taking screenshot for (246x410) to layer 21020
03-04 15:25:35.389: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:35.419: W/Trace(288): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:35.429: W/Trace(288): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:35.438: W/Trace(288): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:35.459: W/Trace(288): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:35.479: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:35.479: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:35.479: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:35.588: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:35.588: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:35.609: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:36.028: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:36.099: I/Choreographer(791): Skipped 31 frames!  The application may be doing too much work on its main thread.
03-04 15:25:36.109: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:36.168: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:36.268: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:36.279: W/Trace(288): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:36.279: W/Trace(288): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:36.289: V/PhoneStatusBar(580): setLightsOn(true)
03-04 15:25:36.299: W/Trace(288): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:36.358: W/Trace(288): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:36.409: E/SurfaceFlinger(36): ro.sf.lcd_density must be defined as a build property
03-04 15:25:36.409: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:36.421: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:36.439: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:36.439: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:36.439: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:36.729: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:36.729: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:36.773: I/Choreographer(791): Skipped 158 frames!  The application may be doing too much work on its main thread.
03-04 15:25:36.779: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:36.779: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:37.209: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:37.209: W/Trace(288): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:37.240: I/ActivityManager(288): Displayed com.example.test1/.clubpage: +1s832ms
03-04 15:25:37.269: W/Trace(288): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:37.269: W/Trace(288): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:37.299: W/Trace(288): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:37.309: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:37.331: W/Trace(288): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:37.399: W/Trace(288): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:37.589: I/Choreographer(791): Skipped 48 frames!  The application may be doing too much work on its main thread.
03-04 15:25:37.649: D/dalvikvm(288): GC_CONCURRENT freed 980K, 35% free 7576K/11552K, paused 9ms+56ms, total 701ms
03-04 15:25:37.859: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:37.859: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:37.911: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:37.911: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:37.919: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:37.949: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:38.214: W/Trace(288): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:38.250: W/Trace(288): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:38.250: W/Trace(288): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:38.309: W/Trace(288): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:38.369: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:38.409: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:38.419: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:38.439: W/Trace(288): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:38.479: W/Trace(288): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:38.519: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:38.530: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:38.530: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:38.549: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:38.549: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:38.549: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:38.549: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:38.629: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:38.629: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:38.690: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:38.699: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:38.699: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:38.719: W/Trace(288): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:38.739: W/Trace(288): Unexpected value from nativeGetEnabledTags: 0
03-04 15:25:38.759: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
4

3 に答える 3

0

HttpPost httppost = new HttpPost( "clubphp");

この行を次のコードに置き換えるだけです

HttpPost httppost = new HttpPost(arg0 [0]);

次のように実行後の実装

@Override
    protected void onPostExecute(String resultName) {
       test.setText(resultName);
    }

問題が発生した場合は、ログ猫をここに投稿するだけで機能することを願っています

于 2013-03-04T09:44:21.313 に答える
0

10.0.2.2 をシステムの IP アドレスに置き換えて、名前を返す前に名前を出力するログを追加してみてください。doInbackground でシステムの IP アドレスを知るには、コマンド プロンプトで ipconfig を実行します。

于 2013-04-28T07:44:37.867 に答える
0

onPostExecute()クラスにメソッドを追加し、Dbhandlerそのメソッドで TextView のテキスト値を設定する必要があります。

public class Dbhandler extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... arg0) {
        [...]
        return name;
    }

    @Override
    protected void onPostExecute(String newName) {
        test.setText(newName);
    }
}
于 2013-03-04T02:15:33.660 に答える