2012-05-04 83 views
1

这是代码,我使用HTTP交HTTP POST给予非法状态异常

public void postData() { 

    //DineOutActivity doa = null; 
    HttpClient httpclient = new DefaultHttpClient(); 
    HttpPost httppost = new HttpPost("192.168.1.112/andro/index.php/androctrl/test/"); 

    try { 

     //food item 
    Spinner spinner = (Spinner) findViewById(R.id.spinner1); 
    String fooditem = spinner.getSelectedItem().toString(); 

    TextView mDateDisplay = (TextView)findViewById(R.id.textView2); 
    String Date = mDateDisplay.toString(); 

    TextView mTimeDisplay = (TextView)findViewById(R.id.textView4); 
    String Time = mTimeDisplay.toString(); 



     // Add your data 
     List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3); 
     nameValuePairs.add(new BasicNameValuePair("fooditem", "fooditem")); 
     nameValuePairs.add(new BasicNameValuePair("Date", "Date")); 
     nameValuePairs.add(new BasicNameValuePair("Time", "Time")); 
     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

     HttpResponse response = httpclient.execute(httppost); 
     String str = inputStreamToString(response.getEntity().getContent()).toString(); 
     Log.w("ADPORTAL", str); 
     HttpStatus status; 

     if(str.toString().equalsIgnoreCase("false")) 
     {Toast.makeText(getBaseContext(), str, Toast.LENGTH_LONG).show(); 
      //result.setText("You are not registerd please register" 
     } 
     else 
     {Toast.makeText(getBaseContext(), str, Toast.LENGTH_LONG).show();} 



    } catch (ClientProtocolException e) { 
     // TODO Auto-generated catch block 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
    } 
} 

private StringBuilder inputStreamToString(InputStream is) { 
    String line = ""; 
    StringBuilder total = new StringBuilder(); 
    // Wrap a BufferedReader around the InputStream 
    BufferedReader rd = new BufferedReader(new InputStreamReader(is)); 
    // Read response until the end 
    try { 
     while ((line = rd.readLine()) != null) { 
      total.append(line); 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    // Return full string 
    return total; 

我使用mTimeDisplay [文本视图2]如从TimePicker对话框的值。 和mDateDisplay [Texview4]作为DatePicker对话框中的值。

以下是我已经使用postData()

public class DineOutActivity extends Activity { 

    private TextView mDateDisplay; 
    private Button mPickDate; 
    private int mYear; 
    private int mMonth; 
    private int mDay; 
    /******************time picker**************/ 
     private TextView mTimeDisplay; 
      private Button mPickTime; 
      private int mHour; 
      private int mMinute; 
      private int mAmPm; 

      static final int TIME_DIALOG_ID=1; 

     static final int DATE_DIALOG_ID = 0; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     /********************spinner***********/ 

     Spinner food = (Spinner) findViewById(R.id.spinner1); 
     ArrayAdapter<CharSequence> foodadapter = ArrayAdapter.createFromResource(
        this, R.array.item_array, android.R.layout.simple_spinner_item); 
     foodadapter.setDropDownViewResource(R.layout.spinner_layout); 
     food.setAdapter(foodadapter); 
     /**pick date*/ 

     mDateDisplay = (TextView) findViewById(R.id.textView2); 
     mTimeDisplay = (TextView) findViewById(R.id.textView4); 
     mPickDate = (Button) findViewById(R.id.button2); 
     /**pick time**/ 

     mPickTime=(Button)findViewById(R.id.button3); 

     // add a click listener to the button 
     mPickTime.setOnClickListener(new View.OnClickListener() { 

        public void onClick(View v) { 

          showDialog(TIME_DIALOG_ID); 
        } 
       }); 

     // get the current time 
     final Calendar c=Calendar.getInstance(); 
     mHour=c.get(Calendar.HOUR_OF_DAY); 
     mMinute=c.get(Calendar.MINUTE); 
     mAmPm = c.get(Calendar.AM_PM); 

     // display the current date 
     upTimeDisplay(); 


/*****************************pick date***********************************/ 
     // add a click listener to the button 
     mPickDate.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v1) { 
       showDialog(DATE_DIALOG_ID); 
      } 
     }); 

     // get the current date 
     final Calendar date = Calendar.getInstance(); 
     mYear = date.get(Calendar.YEAR); 
     mMonth = date.get(Calendar.MONTH); 
     mDay = date.get(Calendar.DAY_OF_MONTH); 
     int mDst = date.get(Calendar.AM_PM); 
     int mAmPm = date.get(Calendar.DST_OFFSET); 

     // display the current date (this method is below) 
     updateDisplay(); 
    } 

    // updates the date in the TextView 




    private String upTimeDisplay() 
    { 
    // mTimeDisplay.setText(new 
      // StringBuilder().append(pad(mHour)).append(":").append(pad(mMinute)).append(pad(mAmPm))); 
     mTimeDisplay.setText(new 
        StringBuilder().append(mHour).append(":").append(mMinute)); 
     mTimeDisplay.setTextColor(R.color.green); 
    return null; 
    } 
    /** private Object pad(int mMinute2) { 

     if(mMinute2>=10) 
       return String.valueOf(mMinute2); 
     else 
       return "0"+String.valueOf(mMinute2); 
}**/ 

    private TimePickerDialog.OnTimeSetListener mtimeSetListener=new 
      TimePickerDialog.OnTimeSetListener() { 

public void onTimeSet(TimePicker view, int hourOfDay, int minute) { 

mHour=hourOfDay; 
mMinute=minute; 
int ampm; 

upTimeDisplay(); 
} 
}; 


    private String updateDisplay() { 
     mDateDisplay.setText(new StringBuilder() 
        // Month is 0 based so add 1 
        .append(mMonth + 1).append("-") 
        .append(mDay).append("-") 
        .append(mYear).append(" ")); 
     mDateDisplay.setTextColor(R.color.green); 
       // .append(mHour).append("_") 
        // .append(mMinute).append("_"))); 
     return null; 
    } 

    // the callback received when the user "sets" the date in the dialog 
    private DatePickerDialog.OnDateSetListener mDateSetListener = 
      new DatePickerDialog.OnDateSetListener() { 

       public void onDateSet(DatePicker view, int year, 
             int monthOfYear, int dayOfMonth) { 
        mYear = year; 
        mMonth = monthOfYear; 
        mDay = dayOfMonth; 
        updateDisplay(); 
       } 
      }; 

    @Override 
    protected Dialog onCreateDialog(int id) { 
     switch (id) { 
     case DATE_DIALOG_ID: 
      return new DatePickerDialog(this, mDateSetListener, mYear, mMonth, 
        mDay); 

     case TIME_DIALOG_ID: 
      return new TimePickerDialog(this,mtimeSetListener,mHour,mMinute,false); 
     } 
     return null; 
    } 







public void imageclick(View view) 
{ 
    postData(); 
    //Intent where = new Intent(this, WhereToeat.class); 
    //startActivity(where); 
} 

/**************/ 

public void postData() { 

    //DineOutActivity doa = null; 
    HttpClient httpclient = new DefaultHttpClient(); 
    HttpPost httppost = new HttpPost("192.168.1.112/andro/index.php/androctrl/test/"); 

    try { 

     //food item 
    Spinner spinner = (Spinner) findViewById(R.id.spinner1); 
    String fooditem = spinner.getSelectedItem().toString(); 

    TextView mDateDisplay = (TextView)findViewById(R.id.textView2); 
    String Date = mDateDisplay.toString(); 

    TextView mTimeDisplay = (TextView)findViewById(R.id.textView4); 
    String Time = mTimeDisplay.toString(); 



     // Add your data 
     List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3); 
     nameValuePairs.add(new BasicNameValuePair("fooditem", "fooditem")); 
     nameValuePairs.add(new BasicNameValuePair("Date", "Date")); 
     nameValuePairs.add(new BasicNameValuePair("Time", "Time")); 
     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

     HttpResponse response = httpclient.execute(httppost); 
     String str = inputStreamToString(response.getEntity().getContent()).toString(); 
     Log.w("ADPORTAL", str); 
     HttpStatus status; 

     if(str.toString().equalsIgnoreCase("false")) 
     {Toast.makeText(getBaseContext(), str, Toast.LENGTH_LONG).show(); 
      //result.setText("You are not registerd please register" 
     } 
     else 
     {Toast.makeText(getBaseContext(), str, Toast.LENGTH_LONG).show();} 



    } catch (ClientProtocolException e) { 
     // TODO Auto-generated catch block 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
    } 
} 

private StringBuilder inputStreamToString(InputStream is) { 
    String line = ""; 
    StringBuilder total = new StringBuilder(); 
    // Wrap a BufferedReader around the InputStream 
    BufferedReader rd = new BufferedReader(new InputStreamReader(is)); 
    // Read response until the end 
    try { 
     while ((line = rd.readLine()) != null) { 
      total.append(line); 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    // Return full string 
    return total; 
} 

我有排队的ExceptionpostData方法

HttpResponse response = httpclient.execute(httppost); 

的logcat中的例外是我的全部代码:

05-04 11:53:00.096: E/AndroidRuntime(4281): FATAL EXCEPTION: main 
05-04 11:53:00.096: E/AndroidRuntime(4281): java.lang.IllegalStateException: Could not execute method of the activity 
05-04 11:53:00.096: E/AndroidRuntime(4281):  at android.view.View$1.onClick(View.java:2144) 
05-04 11:53:00.096: E/AndroidRuntime(4281):  at android.view.View.performClick(View.java:2485) 
05-04 11:53:00.096: E/AndroidRuntime(4281):  at android.view.View$PerformClick.run(View.java:9080) 
05-04 11:53:00.096: E/AndroidRuntime(4281):  at android.os.Handler.handleCallback(Handler.java:587) 
05-04 11:53:00.096: E/AndroidRuntime(4281):  at android.os.Handler.dispatchMessage(Handler.java:92) 
05-04 11:53:00.096: E/AndroidRuntime(4281):  at android.os.Looper.loop(Looper.java:130) 
05-04 11:53:00.096: E/AndroidRuntime(4281):  at android.app.ActivityThread.main(ActivityThread.java:3687) 
05-04 11:53:00.096: E/AndroidRuntime(4281):  at java.lang.reflect.Method.invokeNative(Native Method) 
05-04 11:53:00.096: E/AndroidRuntime(4281):  at java.lang.reflect.Method.invoke(Method.java:507) 
05-04 11:53:00.096: E/AndroidRuntime(4281):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 
05-04 11:53:00.096: E/AndroidRuntime(4281):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625) 
05-04 11:53:00.096: E/AndroidRuntime(4281):  at dalvik.system.NativeStart.main(Native Method) 
05-04 11:53:00.096: E/AndroidRuntime(4281): Caused by: java.lang.reflect.InvocationTargetException 
05-04 11:53:00.096: E/AndroidRuntime(4281):  at java.lang.reflect.Method.invokeNative(Native Method) 
05-04 11:53:00.096: E/AndroidRuntime(4281):  at java.lang.reflect.Method.invoke(Method.java:507) 
05-04 11:53:00.096: E/AndroidRuntime(4281):  at android.view.View$1.onClick(View.java:2139) 
05-04 11:53:00.096: E/AndroidRuntime(4281):  ... 11 more 
05-04 11:53:00.096: E/AndroidRuntime(4281): Caused by: java.lang.IllegalStateException: Target host must not be null, or set in parameters. 
05-04 11:53:00.096: E/AndroidRuntime(4281):  at org.apache.http.impl.client.DefaultRequestDirector.determineRoute(DefaultRequestDirector.java:577) 
05-04 11:53:00.096: E/AndroidRuntime(4281):  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:292) 
05-04 11:53:00.096: E/AndroidRuntime(4281):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555) 
05-04 11:53:00.096: E/AndroidRuntime(4281):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487) 
05-04 11:53:00.096: E/AndroidRuntime(4281):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465) 
05-04 11:53:00.096: E/AndroidRuntime(4281):  at com.crews.dineout.DineOutActivity.postData(DineOutActivity.java:243) 
05-04 11:53:00.096: E/AndroidRuntime(4281):  at com.crews.dineout.DineOutActivity.imageclick(DineOutActivity.java:209) 
05-04 11:53:00.096: E/AndroidRuntime(4281):  ... 14 more 

我从移动运行,并有WiFi连接 而且我有互联网permision。

+0

请不要利用你的标题 “HTTP POST GIVING非法状态例外” 它是不必要的。 – Anirudh

+0

好吧我会编辑它,我正在根据你的答案尝试 – divaNilisha

回答

3

看起来你的URI是无效的。 - http://developer.android.com/reference/java/net/URI.html

你缺少在该计划HTTP

HttpPost httppost = new HttpPost("http://192.168.1.112/andro/index.php/androctrl/test/") 
+0

我正在检查服务器端,到那个时候它是在添加名称值对之前的任何错误/映射 – divaNilisha

+0

我试着用你所说的但数据是被插入为“fooditem”,0000-00-00,00:00 – divaNilisha

+0

如何从日期时间选择器对话框中更改格式 – divaNilisha