2012-04-09 2 views
0

Android 휴대 전화 앱에서 PHP 스크립트에 데이터를 게시하려고합니다. 이것은 내가 사용하고있는 코드입니다. 내 안드로이드 폰에서 실행하려고 할 때Android 앱에서 데이터를 PHP 스크립트에 게시

public class Upload extends Activity implements OnClickListener { 
EditText joke; 
Button upload; 
AlertDialog alertDialog = new AlertDialog.Builder(this).create(); 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.upload); 
} 
public void onClick(View v) { 
    // TODO Auto-generated method stub 
    switch (v.getId()) { 
    case R.id.bUpload: 
     uploadJoke(); 
     break; 
    } 

} 
private void uploadJoke() { 
    // Create a new HttpClient and Post Header 
    HttpClient httpclient = new DefaultHttpClient(); 
    HttpPost httppost = new HttpPost("http://xxxx/telejoke.php"); 

    try { 
     // Add your data 
     List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
     nameValuePairs.add(new BasicNameValuePair("username", "test")); 
     nameValuePairs.add(new BasicNameValuePair("joke", joke.getText().toString())); 
     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

     // Execute HTTP Post Request 
     HttpResponse response = httpclient.execute(httppost); 

    } catch (ClientProtocolException e) { 
     alertDialog.setTitle("Client Protocol Exception"); 
    } catch (IOException e) { 
     alertDialog.setTitle("IOException"); 
    } 
} 

}

그러나, 그것은 단지 예기치 않은 오류와 충돌합니다. 이것에 대한 도움을 주시면 감사하겠습니다.

+0

Hello moesef, plz 오류 로그를 게시하십시오. 변경 사항을 제안 할 수 있습니다. –

+0

메인 스레드에서 네트워크 작업을 실행하고 있습니다. 새 스레드 또는 AsyncTask에서 네트워크 작업을 이동 하시겠습니까? –

+0

오류가 있습니다. AlertDialog와 관련있다. AlertDialog를 uploadJoke 메서드 안에서 움직이면 에러가 발생하지만 여전히 데이터가 PHP 스크립트에 게시되고 있다고 생각하지 않습니다. – moesef

답변

0

UploadJoke 메서드 내에서 AlertDialog alertDialog = new AlertDialog.Builder(this).create();으로 이동하면이 문제가 해결됩니다.

관련 문제