好的,我无缘无故地收到此错误。因为我的应用程序启动,一切正常。 但是当它想要发送HTTP请求时它崩溃并且logcat显示这个错误。
我只在真实设备上收到此错误,它在模拟器中运行良好。
我的应用确实不需要这样的权限。这很简单。 我知道我在代码中做错了,这与这些权限无关。 但我无法弄清楚我在哪里做错了。
这是我的清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ir.Ava.android"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="5"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="false"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.Avatheme"
android:uiOptions="splitActionBarWhenNarrow" android:supportsRtl="true">
<!-- Splash screen -->
<activity
android:name="ir.Ava.android.SplashScreen"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Black.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Main activity -->
<activity
android:name="ir.Ava.android.MainActivity"
android:label="@string/app_name" >
</activity>
<activity android:name="ir.Ava.android.Bubble"></activity>
</application>
</manifest>
这是导致问题的片段:
inside MainActivity.java
public static class profileSectionFragment extends Fragment {
TextView testText;
// flag for Internet connection status
Boolean isInternetPresent = false;
Button editPFButton;
// Connection detector class
ConnectionDetector cd;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.profile, container, false);
((TextView) rootView.findViewById(R.id.pf_username)).setText("Name of the user");
testText = (TextView) rootView.findViewById(R.id.testText);
// Buttons
editPFButton = (Button) rootView.findViewById(R.id.edit_profile);
editPFButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Context appContext = getActivity().getApplicationContext();
// creating connection detector class instance
cd = new ConnectionDetector(appContext);
// get Internet status
isInternetPresent = cd.isConnectingToInternet();
// check for Internet status
if (isInternetPresent) {
testText.setText("successfully connected to the internet");
} else {
testText.setText("there is no internet connection");
}
// Creating HTTP client
HttpClient httpClient = new DefaultHttpClient();
// Creating HTTP Post
HttpPost httpPost = new HttpPost(
"http://192.168.1.2/android/");
// Building post parameters
// key and value pair
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2);
nameValuePair.add(new BasicNameValuePair("email", "user@gmail.com"));
nameValuePair.add(new BasicNameValuePair("message",
"Hi, trying Android HTTP post!"));
// Url Encoding the POST parameters
try {
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
} catch (UnsupportedEncodingException e) {
// writing error to Log
e.printStackTrace();
}
// Making HTTP Request
try {
HttpResponse response = httpClient.execute(httpPost);
Log.d("HTTP Response:", response.toString());
if (response.getStatusLine().getStatusCode() == 200)
{
HttpEntity entity = response.getEntity();
testText.setText(EntityUtils.toString(entity));
}
else
{
testText.setText("Error: "+response.getStatusLine().getStatusCode());
}
} catch (ClientProtocolException e) {
// writing exception to log
e.printStackTrace();
} catch (IOException e) {
// writing exception to log
e.printStackTrace();
}
}
});
return rootView;
}
}
答案 0 :(得分:1)
不允许在主(UI)线程上进行网络I / O.您应该使用AsyncTask
代替。例如:
final TextView testText = ...
AsyncTask<Void, Void, String> task = new AsyncTask<Void, Void, String>()
{
@Override
protected String doInBackground(Void... params)
{
... Create HTTP client, and everything as before ...
// Making HTTP Request
HttpResponse response = httpClient.execute(httpPost);
Log.d("HTTP Response:", response.toString());
if (response.getStatusLine().getStatusCode() == 200)
{
HttpEntity entity = response.getEntity();
return EntityUtils.toString(entity);
}
else
{
return "Error: "+response.getStatusLine().getStatusCode();
}
}
@Override
protected void onPostExecute(String result)
{
testText.setText(result);
}
};
task.execute();