2013-03-15 16 views
0

我試圖從我的Android應用程序發送數據並根據輸入接收輸出。但是,只要我點擊按鈕,應用程序就會關閉。 這裏採用的是Android我的Java文件代碼:發送和接收android和php webservices之間的json - 強制關閉

package com.example.diary; 


import java.io.IOException; 
import java.util.ArrayList; 
import java.util.List; 

import org.apache.http.NameValuePair; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.ResponseHandler; 
import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.BasicResponseHandler; 
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.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ProgressBar; 
import android.widget.Toast; 

public class MainActivity extends Activity implements OnClickListener 
{ 
    private EditText value; 
    private Button btn; 
    private ProgressBar pb; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     value=(EditText)findViewById(R.id.editText1); 
     btn=(Button)findViewById(R.id.button1); 
     pb=(ProgressBar)findViewById(R.id.progressBar1); 
     pb.setVisibility(View.GONE); 
     btn.setOnClickListener(this); 
    } 
    @Override 
    public void onClick(View arg0) { 
     // TODO Auto-generated method stub 
        if(value.getText().toString().length()<1){ 

         // out of range 
         Toast.makeText(this, "please enter something", Toast.LENGTH_LONG).show(); 
        }else{ 
         pb.setVisibility(View.VISIBLE); 
         new MyAsyncTask().execute(value.getText().toString());  
        } 

    } 
    private class MyAsyncTask extends AsyncTask<String, Integer, Double>{ 

     @Override 
     protected Double doInBackground(String... params) { 
      // TODO Auto-generated method stub 
      postData(params[0]); 
      return null; 
     } 

     protected void onPostExecute(Double result){ 
      pb.setVisibility(View.GONE); 
      Toast.makeText(getApplicationContext(), "Code Sent", Toast.LENGTH_LONG).show(); 
     } 
     protected void onProgressUpdate(Integer... progress){ 
      pb.setProgress(progress[0]); 
     } 
     public void postData(String valueIWantToSend) { 

      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost("http://10.0.2.2/remoteaccess/index.php"); 
      //HttpPost httppost = new HttpPost("http://192.168.1.13/educlinic/Widget/AndroidApp"); 
      try { 

       // Add your data 
       List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
       nameValuePairs.add(new BasicNameValuePair("myHttpData", valueIWantToSend)); 
       httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
       ResponseHandler<String> responseHandler = new BasicResponseHandler(); 
       String response = httpclient.execute(httppost, responseHandler); 

       //This is the response from a php application 
       String reverseString = response; 
       Toast.makeText(getApplicationContext(), "response" + reverseString, Toast.LENGTH_LONG).show(); 

       } catch (ClientProtocolException e) { 
       Toast.makeText(getApplicationContext(), "CPE response " + e.toString(), Toast.LENGTH_LONG).show(); 
       // TODO Auto-generated catch block 
       } catch (IOException e) { 
       Toast.makeText(getApplicationContext(), "IOE response " + e.toString(), Toast.LENGTH_LONG).show(); 
       // TODO Auto-generated catch block 
       } 
     } 

}} 

,這是一種用於XML文件中的代碼:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity" > 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/hello_world" 
     android:id="@+id/text1"/> 

    <EditText 
     android:id="@+id/editText1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/text1" 
     android:layout_below="@+id/text1" 
     android:layout_marginLeft="21dp" 
     android:layout_marginTop="22dp" 
     android:ems="10" > 

     <requestFocus /> 
    </EditText> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/editText1" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="59dp" 
     android:text="Button" /> 

    <ProgressBar 
     android:id="@+id/progressBar1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/button1" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="44dp" /> 

</RelativeLayout> 

,這是PHP:提前

<?php 
$data = $_POST['myHttpData']; 
echo $data; 
?> 

感謝。

+0

份額日誌貓輸出。 – 2013-03-15 06:22:25

+1

在這裏發佈你的logcat跟蹤..所以我們可以幫助你更好的解決方案。 – deepa 2013-03-15 06:22:53

+0

03-15 10:25:09.307:我/編舞(3738):跳過了43幀!應用程序可能在其主線程上做了太多工作。 03-15 10:25:57.087:我/編舞(3738):跳過了50幀!應用程序可能在其主線程上做了太多工作。 03-15 10:26:37.807:I/Process(3738):發送信號。 PID:3738 SIG:9 03-15 10:26:47.737:E/Trace(3822):打開跟蹤文件時出錯:無此文件或目錄(2) 03-15 10:26:48.867:D/gralloc_goldfish 3822):未檢測到GPU仿真的仿真器。 – 2013-03-15 10:25:36

回答

1

最有可能你得到以下錯誤:

RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

postData方法,你都試圖表明Toast's.but您呼叫從非UI線程postData方法(從的AsyncTask的doInBackground方法)。

剛剛從postData方法去除吐司的或使用onPostExecute訪問或更新UI元素

+0

感謝您的回答。儘管顯示輸出是烤麪包,但我已經使用了textview。當我調試這個時,顯示的是相同的,但在運行時不顯示。 – 2013-03-15 10:11:40

+0

@NehaMangla:我沒有得到你的問題解決或仍然面臨任何問題? – 2013-03-15 10:17:42

+0

不要使用TextView。使用日誌()。 – greenapps 2013-03-15 11:36:24

相關問題