Java 使用json在android中进行http连接时出错

Java 使用json在android中进行http连接时出错,java,android,json,http,Java,Android,Json,Http,我为保存姓名、生日和喜爱的颜色创建了一个数据库, 然后我创建了一个自定义类和数组,用于在Android中使用JSON读取它, 但是我收到错误HTTP连接 manifest.xml文件 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.customarraylist" and

我为保存姓名、生日和喜爱的颜色创建了一个数据库, 然后我创建了一个自定义类和数组,用于在Android中使用JSON读取它, 但是我收到
错误HTTP连接

manifest.xml文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.customarraylist"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.customarraylist.Main"
            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>

</manifest>
欢迎使用,就像一些用户建议的那样,使用an进行连接,以避免:

这里有一些例子:


当您提出问题时,请尝试发布stacktrace,它显示在LogCat上,以获得有关问题的更多信息=)。

欢迎使用Stack Overflow!你试过什么了吗?确切的错误及其所在的行是什么?它将告诉您更多有关问题原因的信息,而不是使用Toast来表示存在错误打印异常stacktrace并读取它。e、 printStackTrace()作为第一件事,或者您可以使用Log.e(…)作为NetworkOnMainThreadException的可能副本。我的日志没有显示任何错误他不能显示stacktrace,因为他没有打印它。我会先解决这个问题。stackoverflow不允许我发送日志文件:(我的声誉超过10我发送给你的帮助elenasys在这行下面“}catch(异常e){”添加这行:e.printStackTrace();
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

        <ListView
            android:id="@+id/myLisyView"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
        </ListView>

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:textIsSelectable="false"
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#EE0000" />

    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/birthday"
        android:textIsSelectable="false"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#00EE00" />

    <TextView xmlns:android="http://schemas.android.com/apk/res/android" 
        android:textIsSelectable="false"
        android:id="@+id/favorit_color"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#0000EE" />

</LinearLayout>
package com.customarraylist;

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

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;

import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

@SuppressWarnings("unused")
public class Main extends Activity 
{

    String result = "";
    ArrayList<Person> arrayOfWebdata = new ArrayList<Person>();

    class Person
    {
        public String person_id;
        public String name;
        public String favorite_color;
        public String birthday; 


    }

    FancyAdapter aa = null;

    static ArrayList<String> resultRow;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        try {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            // http post
            try {
                HttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost("my php file address");
                HttpResponse response = httpClient.execute(httpPost);
                HttpEntity entity = response.getEntity();
                InputStream webs = entity.getContent();

                // convert response to string
                try {
                    BufferedReader reader = new BufferedReader(new InputStreamReader(webs,"iso-8859-1"),8);
                    StringBuilder sb = new StringBuilder();
                    String line = null;
                    while ((line = reader.readLine()) != null) {
                        sb.append(line + "\n");
                    }
                    // close input stream
                    webs.close();
                    // convert sb to a string
                    result = sb.toString();
                } catch (Exception e) {
                    Toast.makeText(Main.this, "Error converting result ..... ", Toast.LENGTH_LONG).show();
                }// end of catch 3

            } catch (Exception e) {
                Toast.makeText(Main.this, "Error in http connection ..... ", Toast.LENGTH_LONG).show();
            } // end of catch 2

            // parse json data
            try {
                JSONArray jArray = new JSONArray(result);
                for (int i = 0; i < jArray.length(); i++) {
                    JSONObject json_data = jArray.getJSONObject(i);
                    // create a new person
                    Person resultRow = new Person();
                    // set person attrib
                    resultRow.person_id = json_data.getString("person_id");
                    resultRow.name = json_data.getString("name");
                    resultRow.favorite_color = json_data.getString("favorite_color");
                    resultRow.birthday = json_data.getString("birthday");
                    arrayOfWebdata.add(resultRow);
                }

            } catch (Exception e) {
                Toast.makeText(Main.this, "Error parseing data ..... ", Toast.LENGTH_LONG).show();
            }// end catch 4

            ListView myListView = (ListView) findViewById(R.id.myLisyView);

            aa = new FancyAdapter();

            myListView.setAdapter(aa);




        } catch (Exception e) {
            Toast.makeText(Main.this, "Error In Code ..... ", Toast.LENGTH_LONG).show();        
        } // end catch 1
    } // end oncreate

    class FancyAdapter extends ArrayAdapter<Person>
    {
        FancyAdapter()
        {
            super(Main.this, android.R.layout.simple_list_item_1, arrayOfWebdata);
        }

        public View getView (int position, View convertView, ViewGroup parent)
        {
            ViewHolder holder;

            if (convertView == null)
            {
                LayoutInflater inflater = getLayoutInflater();
                convertView = inflater.inflate(R.layout.row, null);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            }
            else
            {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.populateForm(arrayOfWebdata.get(position));

            return (convertView);
        }
    } // end class FancyAdapter

    class ViewHolder
    {
        public TextView name = null;
        public TextView birthday = null;
        public TextView favorite_color = null;

        ViewHolder (View row)
        {
            name = (TextView) findViewById(R.id.name);
            birthday = (TextView) findViewById(R.id.birthday);
            favorite_color = (TextView) findViewById(R.id.favorit_color);

        }

        void populateForm(Person r)
        {
            name.setText(r.name);
            birthday.setText(r.birthday);
            favorite_color.setText(r.favorite_color);

        }
    }
} // end main activity
03-31 20:48:38.901: E/InputDispatcher(783): channel '41b82438 com.customarraylist/com.customarraylist.Main (server)' ~ Channel is unrecoverably broken and will be disposed!
03-31 20:48:43.736: E/dalvikvm(13139): Could not find class 'android.telephony.CellInfoWcdma', referenced from method com.facebook.common.hardware.CellDiagnosticsSerializer.c
03-31 20:48:44.557: E/dalvikvm(13219): Could not find class 'android.test.IsolatedContext', referenced from method com.sonyericsson.extras.liveware.db.ExperienceDatabaseHelper.onUpgrade
03-31 20:48:46.028: E/FmProxy(13344): Could not bind to IFmReceiverService Service
03-31 20:48:46.509: E/(13372): netstack: LIB_MGR - Error loading lib spl_proc_plugin.so
03-31 20:48:46.509: E/(13372): netstack: STAT_HUB - Failed to load plugin: spl_proc_plugin.so
03-31 20:48:46.519: E/(13372): netstack:  STAT_HUB - App com.yahoo.mobile.client.android.mail isn't supported
03-31 20:48:49.552: E/dalvikvm(13642): Could not find class 'android.app.AppOpsManager', referenced from method box.a
03-31 20:48:49.862: E/ObjectHelper(13719): Can't find method:setCompatibilityInfo
03-31 20:48:51.254: E/(13797): netstack: LIB_MGR - Error loading lib spl_proc_plugin.so
03-31 20:48:51.254: E/(13797): netstack: STAT_HUB - Failed to load plugin: spl_proc_plugin.so
03-31 20:48:51.254: E/(13797): netstack:  STAT_HUB - App com.google.android.apps.magazines isn't supported
03-31 20:49:03.277: E/EventHub(783): /dev/input/event4 KEYPAD_EVENT: got: t0=44254, t1=822809, type=1, code=116, value=1
03-31 20:49:03.417: E/wifi(783): reply: RSSI=-52
03-31 20:49:03.417: E/wifi(783): LINKSPEED=72
03-31 20:49:03.417: E/wifi(783): NOISE=9999
03-31 20:49:03.417: E/wifi(783): FREQUENCY=0
03-31 20:49:03.447: E/EventHub(783): /dev/input/event4 KEYPAD_EVENT: got: t0=44254, t1=995310, type=1, code=116, value=0
03-31 20:49:04.818: E/qdlights(783): LED: set_light_notification()- somc_use:0
03-31 20:49:04.818: E/qdlights(783): [LED]start battery LED
03-31 20:49:04.818: E/qdlights(783): [set_speaker_light_locked]noti_ena:1
03-31 20:49:04.818: E/qdlights(783): LED: android LED->blink=0, red = 170, green = 32, blue = 0, onMS=0, offMS=0
03-31 20:49:04.818: E/qdlights(783): LED: resume_previous_LED, bat_status:2 and resume charging LED!
03-31 20:49:06.440: E/wifi(783): reply: RSSI=-45
03-31 20:49:06.440: E/wifi(783): LINKSPEED=39
03-31 20:49:06.440: E/wifi(783): NOISE=9999
03-31 20:49:06.440: E/wifi(783): FREQUENCY=0
03-31 20:49:09.453: E/wifi(783): reply: RSSI=-48
03-31 20:49:09.453: E/wifi(783): LINKSPEED=39
03-31 20:49:09.453: E/wifi(783): NOISE=9999
03-31 20:49:09.453: E/wifi(783): FREQUENCY=0
03-31 20:49:12.456: E/wifi(783): reply: RSSI=-49
03-31 20:49:12.456: E/wifi(783): LINKSPEED=39
03-31 20:49:12.456: E/wifi(783): NOISE=9999
03-31 20:49:12.456: E/wifi(783): FREQUENCY=0
03-31 20:49:15.470: E/wifi(783): reply: RSSI=-48
03-31 20:49:15.470: E/wifi(783): LINKSPEED=39
03-31 20:49:15.470: E/wifi(783): NOISE=9999
03-31 20:49:15.470: E/wifi(783): FREQUENCY=0
03-31 20:49:18.483: E/wifi(783): reply: RSSI=-48
03-31 20:49:18.483: E/wifi(783): LINKSPEED=52
03-31 20:49:18.483: E/wifi(783): NOISE=9999
03-31 20:49:18.483: E/wifi(783): FREQUENCY=0
03-31 20:49:21.496: E/wifi(783): reply: RSSI=-48
03-31 20:49:21.496: E/wifi(783): LINKSPEED=52
03-31 20:49:21.496: E/wifi(783): NOISE=9999
03-31 20:49:21.496: E/wifi(783): FREQUENCY=0
03-31 20:49:24.499: E/wifi(783): reply: RSSI=-48
03-31 20:49:24.499: E/wifi(783): LINKSPEED=52
03-31 20:49:24.499: E/wifi(783): NOISE=9999
03-31 20:49:24.499: E/wifi(783): FREQUENCY=0
03-31 20:49:27.512: E/wifi(783): reply: RSSI=-49
03-31 20:49:27.512: E/wifi(783): LINKSPEED=52
03-31 20:49:27.512: E/wifi(783): NOISE=9999
03-31 20:49:27.512: E/wifi(783): FREQUENCY=0
03-31 20:49:30.526: E/wifi(783): reply: RSSI=-50
03-31 20:49:30.526: E/wifi(783): LINKSPEED=52
03-31 20:49:30.526: E/wifi(783): NOISE=9999
03-31 20:49:30.526: E/wifi(783): FREQUENCY=0
03-31 20:49:33.529: E/wifi(783): reply: RSSI=-49
03-31 20:49:33.529: E/wifi(783): LINKSPEED=52
03-31 20:49:33.529: E/wifi(783): NOISE=9999
03-31 20:49:33.529: E/wifi(783): FREQUENCY=0
03-31 20:49:36.542: E/wifi(783): reply: RSSI=-49
03-31 20:49:36.542: E/wifi(783): LINKSPEED=52
03-31 20:49:36.542: E/wifi(783): NOISE=9999
03-31 20:49:36.542: E/wifi(783): FREQUENCY=0
03-31 20:49:37.083: E/qdlights(783): LED: set_light_notification()- somc_use:0
03-31 20:49:37.083: E/qdlights(783): [LED]start notification LED
03-31 20:49:37.083: E/qdlights(783): [set_speaker_light_locked]noti_ena:0
03-31 20:49:37.083: E/qdlights(783): LED: android LED->blink=1, red = 170, green = 170, blue = 170, onMS=100, offMS=6000