2013-05-14 3 views
0

이 코드는 여기에 있습니다. 앱을 중지 시키면 문제가 발생합니다. 그렇지 않아! 나는 무엇이 잘못 되었는가를 모릅니다. 누군가 제발 저에게 말해 주시겠습니까? 감사합니다 ..httpSt 앱에서 문제가 발생했습니다.

public class MainActivity extends Activity { 

    EditText msgTextField; 
    Button sendButton; 

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

      //make message text field object 
      msgTextField = (EditText) findViewById(R.id.msgTextField); 
      //make button object 
      sendButton = (Button) findViewById(R.id.sendButton); 
     } 

     public void send(View v) 
     { 
      //get message from message box 
      String msg = msgTextField.getText().toString(); 

      //check whether the msg empty or not 
      if(msg.length()>0) { 
       HttpClient httpclient = new DefaultHttpClient(); 
       HttpPost httppost = new HttpPost("http://192.168.10.28/app/app1.php"); 

       try { 
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
         nameValuePairs.add(new BasicNameValuePair("id", "01")); 
         nameValuePairs.add(new BasicNameValuePair("message", msg)); 
         httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
         httpclient.execute(httppost); 
         msgTextField.setText(""); //reset the message text field 
         Toast.makeText(getBaseContext(),"Sent",Toast.LENGTH_SHORT).show(); 
       } catch (ClientProtocolException e) { 
        e.printStackTrace(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } else { 
       //display message if text field is empty 
       Toast.makeText(getBaseContext(),"All fields are required",Toast.LENGTH_SHORT).show(); 
      } 
     } 

    } 

여기에 나는 Manifes.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.form" 
    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.example.form.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> 

</manifest> 

답변

0

당신은 UI 스레드에서 부르지 말아야 할 것들을하고 있습니다. AsyncTask를 작성하여 이러한 조작을 수행 할 수 있습니다. 자세한 내용은 this을 참조하십시오.

+0

나는 이것에 아주 새롭다. coudl 당신은 저를 나의 부호에서 그것을 실행하는 도와주십시오? – alandr

0

이 모든 차단 호출이 UI Thread 다른 스레드에서 실행해야합니다. NetworkOnMainThreadException을 받고 있습니다. Thread 또는 AsyncTask을 사용하십시오. 읽기 here

관련 문제