2014年12月17日 星期三

Recognizer + Android 4.0↑ HttpClient 實作

***********************************
test3.php
***********************************


<?php

header ("Content-Type:text/html; charset=utf-8");

$response = $_POST['data'];


echo $response;




?>

***********************************
Main.java
***********************************

public class Main extends Activity implements OnClickListener

{
private String txtMessage;
private Button sendBtn;
private TextView tv;

private String uriAPI = "http://localhost/androidapp/test3.php";
protected static final int REFRESH_DATA = 0x00000001;

@Override
public void onCreate(Bundle savedInstanceState)
{

super.onCreate(savedInstanceState);
setContentView(R.layout.main);


sendBtn = (Button) findViewById(R.id.send_btn);
tv = (TextView)findViewById(R.id.textView1);

if (sendBtn != null)
sendBtn.setOnClickListener(this);
}

Handler mHandler = new Handler()
{
@Override
public void handleMessage(Message msg)
{
switch (msg.what)
{

case REFRESH_DATA:
String result = null;
if (msg.obj instanceof String)
result = (String) msg.obj;
if (result != null)

tv.setText(result);
break;
}
}
};

@Override
public void onClick(View v)

{
if (v == sendBtn)
{
Intent it = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
       it.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
               RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
       it.putExtra(RecognizerIntent.EXTRA_PROMPT, "請說...");
       this.startActivityForResult(it, 1);

}
}

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent it) {
        if (requestCode != 1) {
         
            return;
        }
        if (resultCode != RESULT_OK) {
         
            return;
        }
        List<String> list = it.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
        txtMessage = list.get(0);
     
        this.onclick2(txtMessage);
     
    }

public void onclick2 (String txtMessage) {
if (txtMessage != null)
{

final String msg = txtMessage.toString();

Thread t = new Thread(new sendPostRunnable(msg));
t.start();
}
}

class sendPostRunnable implements Runnable
{
String strTxt = null;

public sendPostRunnable(String strTxt)
{
this.strTxt = strTxt;
}

@Override
public void run()
{
String result = sendPostDataToInternet(strTxt);
mHandler.obtainMessage(REFRESH_DATA, result).sendToTarget();
}

}

private String sendPostDataToInternet(String strTxt)
{

HttpPost httpRequest = new HttpPost(uriAPI);

List<NameValuePair> params = new ArrayList<NameValuePair>();

params.add(new BasicNameValuePair("data", strTxt));

try

{
httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));

HttpResponse httpResponse = new DefaultHttpClient()
.execute(httpRequest);

if (httpResponse.getStatusLine().getStatusCode() == 200)
{

String strResult = EntityUtils.toString(httpResponse
.getEntity());

return strResult;
}
} catch (Exception e)
{
e.printStackTrace();
}
return null;
}

}


***********************************
main.xml
***********************************

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/send_btn"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/send" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>


***********************************
AndroidManifest.xml
***********************************

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.J_Test.httpPostTest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".Main"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <!-- 這裡加入可以存取網路的權限 -->

    <uses-permission android:name="android.permission.INTERNET" />

</manifest>













Android 2.2 ↑↓請看:
android 2.2 ↑↓ 版使用 HtppClient

沒有留言:

張貼留言