2013-08-20 4 views
0

httpPost 요청을 사용하여 만든 localhost (WAMP) 웹 서비스에 연결하려고합니다. 그것은 계속 실패하고 IllegalStateExecption을 제공합니다. 누구나 문제에 대한 통찰력을 제공 할 수 있습니까? 고맙습니다!HttpPost가 Illegalstateexception을 발생합니다.

String songtext,artisttext; 
HttpResponse response ; 
EditText song; 
EditText artist; 
EditText party; 
HttpPost post; 
HttpClient client; 
ArrayList<NameValuePair> nameValuePairs; 
String URL = "http://10.0.2.2/GoDJ/index.php?r=request/create"; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    song = (EditText)findViewById(R.id.songTitle); 
    artist = (EditText)findViewById(R.id.songArtist); 
    party = (EditText)findViewById(R.id.partyid); 



} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

public void uploadToDB(View view) 
{ 
    //send user message 
    Context context = getApplicationContext(); 
    CharSequence text = "Letting the DJ Know!!"; 
    int duration = Toast.LENGTH_SHORT; 

    Toast toast = Toast.makeText(context, text, duration); 
    toast.show(); 
    //add values 
    nameValuePairs = new ArrayList<NameValuePair>(); 
    nameValuePairs.add(new BasicNameValuePair("party", party.getText().toString())); 
    nameValuePairs.add(new BasicNameValuePair("title", song.getText().toString())); 
    nameValuePairs.add(new BasicNameValuePair("artist", artist.getText().toString())); 

    try 
    { 
     //instantiate request 
     client = new DefaultHttpClient(); 
     post = new HttpPost(URL); 
     text = "Set Up Client and Post"; 
     toast = Toast.makeText(context, text, duration); 
     toast.show(); 

     post.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
     text = "Entity Set"; 
     toast = Toast.makeText(context, text, duration); 
     toast.show(); 
     response = client.execute(post); 
     text = "Post Executed SUCCESS"; 
     toast = Toast.makeText(context, text, duration); 
     toast.show(); 

    } 

    catch(Exception e) 
    { 
     text = "FAILURE"; 
     toast = Toast.makeText(context, text, duration); 
     toast.show(); 
     e.printStackTrace(); 
    } 
} 

}

답변

0

당신은 안드로이드 4 및 네트워킹

Pls는 허용되지 UI 스레드에서 실행되고 있기 때문이다는 비동기 작업을 사용하고 문제의 유일한 것이다.

Here의 사용 예입니다.

0

쉬운 방법은 this lib 을 사용하고 후불을 사용하는 방법은 @Background 주석을 사용하는 방법입니다.

관련 문제