Android HttpPost通过Yii Framework返回500

时间:2012-02-21 23:17:48

标签: android .htaccess yii http-post

我有一个服务器端应用程序,它运行在Yii Framework上。我想在Android上使用Yii访问数据库(MySQL)。

所以我这样从客户发帖:

HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("myurl/reach");

try {
    List<NameValuePair> nmp = new ArrayList<NameValuePair>();
    ResponseHandler<String> responseHandler=new BasicResponseHandler();

    // Parameters are here 
    // nmp.add(new BasicNameValuePair("name", "value"));

    httpPost.setEntity(new UrlEncodedFormEntity(nmp));
    String rs = httpClient.execute(httpPost, responseHandler);
} catch (ClientProtocolException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

这是 Yii的 urlManager规则:

'/reach' => 'site/reach',

最后actionReach()上的siteController方法:

if(Yii::app()->request->isPostRequest)
{
    // Reach MySQL 
} else
{
    $this->redirect(Yii::app()->homeUrl);   
}

当我尝试运行应用程序时,请{get org.apache.http.client.HttpResponseException: Internal Server Error

这是所有堆栈:

02-22 01:11:18.803: W/System.err(3924): org.apache.http.client.HttpResponseException: Internal Server Error
02-22 01:11:18.823: W/System.err(3924):     at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:71)
02-22 01:11:18.823: W/System.err(3924):     at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:59)
02-22 01:11:18.823: W/System.err(3924):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:657)
02-22 01:11:18.823: W/System.err(3924):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:627)
02-22 01:11:18.828: W/System.err(3924):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:616)
02-22 01:11:18.828: W/System.err(3924):     at com.app.myapp.PostActivity$postInfoTask.doInBackground(PostActivity.java:175)
02-22 01:11:18.828: W/System.err(3924):     at com.app.myapp.PostActivity$postInfoTask.doInBackground(PostActivity.java:1)
02-22 01:11:18.828: W/System.err(3924):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
02-22 01:11:18.833: W/System.err(3924):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
02-22 01:11:18.833: W/System.err(3924):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
02-22 01:11:18.833: W/System.err(3924):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
02-22 01:11:18.833: W/System.err(3924):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
02-22 01:11:18.838: W/System.err(3924):     at java.lang.Thread.run(Thread.java:1096)

最后但并非最不重要的是,当我将我的网址 reach/更改为reach时,没有错误,但即使我已经发布,Yii也会返回主页html输出。

我错过了什么?它是关于.htaccess还是什么?

1 个答案:

答案 0 :(得分:2)

经过长时间的研究,我找到了答案。不幸的是,.htaccess以某种方式阻止了$_POST。所以我决定将我的行动转移到现有控制器,它已经很好

所以,首先将我的路由规则改为:

'reach/' => 'existed/reach',

在existedController中,我写了一个名为reachAction的新动作:

if(Yii::app()->request->isPostRequest)
{
    // MySQL Insert
}

此if语句没问题。最后,我从我的模型中获得了一个实例并使用了$model->save();

就是这样。