gpt4 book ai didi

java - android中使用json进行http连接时出错

转载 作者:行者123 更新时间:2023-12-01 13:12:09 25 4
gpt4 key购买 nike

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

ma​​nifest.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>

ma​​in.xml

<?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>

row.xml

<?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>

Main.java

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

最佳答案

欢迎来到Stackoverflow ,就像一些用户建议使用 Asynctask建立连接,避免 NetworkOnMainThreadException :

这里有一些例子:

AsyncTask Android example

Android Background Processing

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

关于java - android中使用json进行http连接时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22766006/

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