我正在尝试将我的android连接到php。我在互联网上找到了一些代码,但它一直给我这个例外。这是我的主要活动
public class MainActivity extends ActionBarActivity {
TextView tv;
String text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView)findViewById(R.id.textview);
text = "";
try {
postData();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void postData() throws JSONException{
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.99.7.91/post.php");
JSONObject json = new JSONObject();
try {
// JSON data:
json.put("name", "puffles");
json.put("position", "lame");
JSONArray postjson=new JSONArray();
postjson.put(json);
// Post the data:
httppost.setHeader("json",json.toString());
httppost.getParams().setParameter("jsonpost",postjson);
// Execute HTTP Post Request
System.out.print(json);
HttpResponse response = httpclient.execute(httppost);
// for JSON:
if(response != null)
{
InputStream is = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
text = sb.toString();
}
tv.setText(text);
}catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
这是我的manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.fatima.scmyproject" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
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">
</uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
</manifest>
这是activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ScrollView android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="@+id/scrollView1">
<LinearLayout android:layout_width="match_parent"
android:id="@+id/linearLayout1"
android:layout_height="match_parent">
<TextView android:id="@+id/textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="hellow" />
</LinearLayout>
</ScrollView>
</LinearLayout>
到目前为止,我的代码无法解决问题。我在xml文件中添加了textview,确保在调用findViewById()之前调用setcontentview()。我还没有删除所有自动生成的方法。请告诉我问题是什么。
答案 0 :(得分:0)
Android不建议在主线程上调用网络调用,这就是为什么你在主要使用异步任务时调用网络调用而不是调用网络调用,或者你想调用主线程只需在setContentView之后的onCreate()活动中添加以下行()
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);