Php HttpPost提供非法状态异常

Php HttpPost提供非法状态异常,php,android,Php,Android,我正在尝试连接到使用httpPost请求创建的localhost(WAMP)Web服务。它不断失败,并给予非法的国家执行权。有人能对这个问题提供一些见解吗?谢谢大家! String songtext,artisttext; HttpResponse response ; EditText song; EditText artist; EditText party; HttpPost post; HttpClient client; ArrayList<NameValuePair> n

我正在尝试连接到使用httpPost请求创建的localhost(WAMP)Web服务。它不断失败,并给予非法的国家执行权。有人能对这个问题提供一些见解吗?谢谢大家!

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();
    }
}
String songtext,artisttext;
HttpResponse响应;
编辑文本歌曲;
编辑文本艺术家;
编辑文本党;
HttpPost;
HttpClient;
ArrayList名称值对;
字符串URL=”http://10.0.2.2/GoDJ/index.php?r=request/create";
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
song=(编辑文本)findViewById(R.id.songTitle);
艺术家=(编辑文本)findViewById(R.id.songArtist);
party=(EditText)findViewById(R.id.partyid);
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.main,menu);
返回true;
}
公共无效上载TODB(视图)
{
//发送用户消息
Context=getApplicationContext();
CharSequence text=“让DJ知道!!”;
int duration=Toast.LENGTH\u SHORT;
Toast Toast=Toast.makeText(上下文、文本、持续时间);
toast.show();
//增加价值
nameValuePairs=新的ArrayList();
添加(新的BasicNameValuePair(“party”,party.getText().toString());
添加(新的BasicNameValuePair(“title”,song.getText().toString());
添加(新的BasicNameValuePair(“艺术家”,artist.getText().toString());
尝试
{
//实例化请求
client=新的DefaultHttpClient();
post=新的HttpPost(URL);
text=“设置客户端和Post”;
toast=toast.makeText(上下文、文本、持续时间);
toast.show();
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
text=“实体集”;
toast=toast.makeText(上下文、文本、持续时间);
toast.show();
响应=client.execute(post);
text=“执行后成功”;
toast=toast.makeText(上下文、文本、持续时间);
toast.show();
}
捕获(例外e)
{
text=“失败”;
toast=toast.makeText(上下文、文本、持续时间);
toast.show();
e、 printStackTrace();
}
}

}

这是因为您运行的是Android 4,而UI线程不允许联网

请使用异步任务,它将解决问题

这是一个如何使用它的示例。

一个简单的方法就是使用它 并使用
@Background
注释的方法进行post调用