2013-08-20 33 views
0

我想連接到我使用httpPost請求創建的本地主機(WAMP)web服務。它一直失敗,並給出一個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

這是因爲你在Android 4和不允許網絡

PLS UI線程中運行使用異步任務,它會唯一的問題。

Here是一個如何使用它的例子。

0

一個簡單的方法是使用this lib 並用方法使用@Background註釋做後調用。

相關問題