我有一個應用程序調用Web服務來獲取PNR狀態。但我是創建網絡服務的新手,所以我已經通過一些視頻和示例來了解如何創建和使用Web服務。創建Web服務時遇到問題
我在stackoverflowhttps://stackoverflow.com/questions/14598485/need-indian-railway-apis-for-making-app-for-pnr-status-train-info-etc和這個問題中提供的所有鏈接,我得到這個:http://pnrapi.alagu.net/。
我的代碼是:
XML:activity_status_pnr.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText android:id="@+id/et_pnr"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/h"
android:maxLength="10" />
<Button android:id="@+id/pnr_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/gstatus"
android:layout_gravity="center" />
</LinearLayout>
Java代碼:StatusPNR.java
package com.example.infopnr;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import java.io.IOException;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
public class StatusPNR extends Activity {
String URL = "http://pnrapi.alagu.net/api/v1.0/pnr/UR_PNR_CODE";
String result = "";
//String deviceId = "xxxxx" ;
//final String tag = "Your Logcat tag: ";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_status_pnr);
final EditText txtSearch = (EditText)findViewById(R.id.et_pnr);
txtSearch.setOnClickListener(new EditText.OnClickListener()
{
public void onClick(View v){txtSearch.setText("");}
});
final Button btnSearch = (Button)findViewById(R.id.pnr_status);
btnSearch.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
String query = txtSearch.getText().toString();
PNRWebService(query);
}
});
}
private void PNRWebService(String q){
}
}
我還沒有創建的Web服務方法尚未作爲我在這裏遇到了一些問題。在某些網站上,我讀到應該使用Apache Web服務器來獲取HTTP客戶端。但是,我不確定,不知道爲什麼會這樣?
我的問題是:
1)是他們的任何需要安裝以獲得web服務器或我們的編碼是足以調用Web服務沒有任何東西任何事先安裝?
2)在PNRWebService()方法中,我需要做什麼才能獲得連接並使用URL調用服務?我知道我需要通過HTTPClient建立連接,但經過許多例子和視頻之後,我並沒有明白他們究竟是怎麼做的?
3)http://pnrapi.alagu.net/與本網站的參考我想獲得PNR狀態?爲了調用這個我需要做的事情,因爲我不知道這個URL是什麼,以及在獲取URL之後還應該做些什麼?
4)我從Apache導入了一些軟件包,我不知道它是否值得?
如果任何人有良好的知識,請幫助他人,然後請讓我知道如何執行。
謝謝帕爾!我已經實現了它,但我想問一下,我想調用ansynTask單擊按鈕,當我創建它像這樣:final Button btnSearch =(Button)findViewById(R.id.pnr_status); btnSearch.setOnClickListener(新Button.OnClickListener(){ 公共無效的onClick(視圖v){ \t \t新的AsyncTask(本).execute(URL); } }); – Saggy
它顯示錯誤:構造函數StatusPNR.asyncTask(新View.OnClickListener(){})未定義....在這裏我不能調用on-clickListener事件的構造函數作爲上下文值在那裏。對? – Saggy
我想不可以在OnClick Event上調用asyncTask,如果沒有,那麼程序將如何知道從他們需要獲取PNR的位置以及點擊他們需要啓動過程的位置?我有點混亂。 – Saggy