另一个Android问题,我试图将数据从我的应用程序上的edittext发布到服务器上的脚本,但是我遇到了一个又一个错误。这是我现在拥有的
package com.college.slimandsave;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;
public class FoodSearchActivity extends Activity implements OnClickListener{
public ListView list;
public Button btnSearch;
private static EditText txtFoodSearch;
private ProgressBar pb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_food_search);
txtFoodSearch = (EditText)findViewById(R.id.txtFoodSearch);
// txtFoodSearch = txtFoodSearch.getText().toString();
btnSearch = (Button)findViewById(R.id.btnSearch);
// btnSearch.setOnClickListener(new View.OnClickListener() {
pb =(ProgressBar)findViewById(R.id.progressBar1);
pb.setVisibility(View.GONE);
btnSearch.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu); // check this
return true;
}
public void onClick(View v) {
if(txtFoodSearch.getText().toString().length()<1) {
Toast.makeText(this , "please enter something", Toast.LENGTH_LONG).show();
}else{
pb.setVisibility(View.VISIBLE);
new MyAsyncTask().execute(txtFoodSearch.getText().toString());
}
}
private class MyAsyncTask extends AsyncTask<String, Integer, Double>{
String fsearch = txtFoodSearch.getText().toString();
@Override
protected Double doInBackground(String... params) {
postData(params[0]);
return null;
}
protected void onPostExecute(Double result) {
pb.setVisibility(View.GONE);
Toast.makeText(getApplicationContext(), "search sent", Toast.LENGTH_LONG).show();
}
protected void onProgressUpdate(Integer...progress) {
pb.setProgress(progress[0]);
}
public void postData(String fsearch) {
//create new http client
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("192.168.133.84/SlimandSave/try.php");
try {
//add data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("myHttpData", fsearch));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
//execute HTTP post request
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
}
}
}
我的清单就是这个
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.college.slimandsave"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="12"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.college.slimandsave.HomeScreenActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.college.slimandsave.StatsScreenActivity"
android:label="@string/lblStatsScreen" >
</activity>
<activity
android:name="com.college.slimandsave.FoodDiaryActivity"
android:label="@string/lblFoodDiary" >
</activity>
<activity
android:name="com.college.slimandsave.SlimAndSaveActivity"
android:label="@string/Slim_And_Save" >
</activity>
<activity
android:name="com.college.slimandsave.AboutScreenActivity"
android:label="@string/lblAbout" >
</activity>
<activity
android:name="com.college.slimandsave.FoodSearchActivity"
android:label="@string/lblFoodSearch" >
</activity>
<activity
android:name="com.college.slimandsave.GetItemsActivity"
android:label="@string/app_name" >
</activity>
</application>
</manifest>
最后是错误日志。
02-19 08:49:25.019: E/AndroidRuntime(5574): FATAL EXCEPTION: AsyncTask #1
02-19 08:49:25.019: E/AndroidRuntime(5574): java.lang.RuntimeException: An error occured while executing doInBackground()
02-19 08:49:25.019: E/AndroidRuntime(5574): at android.os.AsyncTask$3.done(AsyncTask.java:299)
02-19 08:49:25.019: E/AndroidRuntime(5574): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
02-19 08:49:25.019: E/AndroidRuntime(5574): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
02-19 08:49:25.019: E/AndroidRuntime(5574): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
02-19 08:49:25.019: E/AndroidRuntime(5574): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
02-19 08:49:25.019: E/AndroidRuntime(5574): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
02-19 08:49:25.019: E/AndroidRuntime(5574): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
02-19 08:49:25.019: E/AndroidRuntime(5574): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
02-19 08:49:25.019: E/AndroidRuntime(5574): at java.lang.Thread.run(Thread.java:856)
02-19 08:49:25.019: E/AndroidRuntime(5574): Caused by: java.lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=null, host=null, path=192.168.133.84/SlimandSave/try.php
02-19 08:49:25.019: E/AndroidRuntime(5574): at org.apache.http.impl.client.DefaultRequestDirector.determineRoute(DefaultRequestDirector.java:591)
02-19 08:49:25.019: E/AndroidRuntime(5574): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:293)
02-19 08:49:25.019: E/AndroidRuntime(5574): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:670)
02-19 08:49:25.019: E/AndroidRuntime(5574): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:509)
02-19 08:49:25.019: E/AndroidRuntime(5574): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
02-19 08:49:25.019: E/AndroidRuntime(5574): at com.college.slimandsave.FoodSearchActivity$MyAsyncTask.postData(FoodSearchActivity.java:106)
02-19 08:49:25.019: E/AndroidRuntime(5574): at com.college.slimandsave.FoodSearchActivity$MyAsyncTask.doInBackground(FoodSearchActivity.java:81)
02-19 08:49:25.019: E/AndroidRuntime(5574): at com.college.slimandsave.FoodSearchActivity$MyAsyncTask.doInBackground(FoodSearchActivity.java:1)
02-19 08:49:25.019: E/AndroidRuntime(5574): at android.os.AsyncTask$2.call(AsyncTask.java:287)
02-19 08:49:25.019: E/AndroidRuntime(5574): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
02-19 08:49:25.019: E/AndroidRuntime(5574): ... 5 more
编辑.....以上是我最新的代码和错误日志。在php方面,我只有一个fileputcontents来写一个文本文件。
编辑...在ip地址似乎工作之前添加http://而不是使用namevalue对中的fSearch字符串我使用了txtFoodSearch.getText()。toString()。
答案 0 :(得分:2)
SearchActivity
不是活动类
你有
Intent intSearch = new Intent(FoodSearchActivity.this, SearchActivity.class);
startActivity(intSearch);
它是一个AsyncTask类
public static class SearchActivity extends AsyncTask<String, Void, String> {
调用Asynctask使用
new SearchActivity().execute(params);
您的doInbackground
什么也没做。
您在postData(View v)
中有网络操作,应该进入doInbackground
你也有
<activity android:name="com.college.slimandsave.FoodSearchActivity$SearchActivity" >
</activity>
应删除。