第一:抱歉我的英文。
这是我通过PHP从Android连接到MSSQL的代码,它工作正常,但我得到ConnectTimeoutException
,因为即使没有建立连接,程序仍继续运行。
public class JSONParser {
static JSONArray Jarray1 = null;
private static InputStream is1 = null;
private static String json1 = "";
public JSONArray getJSONFromUrl(String url, String[] parametri) throws JSONException {
// Preparo i dati da passare tramite POST
ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("NRCommessa", parametri[0]));
params.add(new BasicNameValuePair("SC", parametri[1]));
// HTTP request
Log.i("NRCommessa", parametri[0]);
Log.i("SC", parametri[1]);
try {
HttpParams httpParams = new BasicHttpParams();
// Timeout per la connessione
int connectionTimeout = 10000; //10 secondi
HttpConnectionParams.setConnectionTimeout(httpParams,connectionTimeout);
// Timeout per i dati
int dataTimeout = 20000; //20 secondi
HttpConnectionParams.setSoTimeout(httpParams, dataTimeout);
DefaultHttpClient httpClient = new DefaultHttpClient(httpParams);
HttpPost httppost = new HttpPost(url);
httppost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httppost);
HttpEntity httpEntity = httpResponse.getEntity();
is1 = httpEntity.getContent();
} catch (ConnectTimeoutException e) {
e.printStackTrace();
json1 = "{'phpCampo1':'Errore','phpCampo2':'connessione','phpCampo3':''}";
} catch (SocketTimeoutException e) {
e.printStackTrace();
json1 = "{'phpCampo1':'Errore','phpCampo2':'ricevimento','phpCampo3':'dati'}";
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is1), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
is1.close();
json1 = sb.toString();
Log.e("JSON", json1);
} catch (Exception e) {
Log.e("Buffer Error", "Errore nella conversione " + e.toString());
}
try {
Jarray1 = new JSONArray(json1);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON Object
return Jarray1;
}
}
这是调用&#34; JSONParser&#34;:
的代码public class LeggiUbicazione extends ListActivity {
String url = "http://192.168.0.8/oca/leggicommessa.php";
private static final String Campo1 = "phpCampo1";
private static final String Campo2 = "phpCampo2";
private static final String Campo3 = "phpCampo3";
ArrayList<HashMap<String, String>> jsonlist = new ArrayList<HashMap<String, String>>();
EditText txtNumeroCommessa, txtSottoCommessa;
Button btnNuovaRicerca, btnSkip;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reucupera_ubicazione);
txtNumeroCommessa = (EditText) findViewById(R.id.leggiNumeroCommessa);
txtSottoCommessa = (EditText) findViewById(R.id.leggiSottoCommessa);
txtNumeroCommessa.setText("");
txtSottoCommessa.setText("");
Button btnNuovaRicerca = (Button) this.findViewById(R.id.btnNuovaRicerca);
btnSkip = (Button) findViewById(R.id.SkipSottoComm);
btnNuovaRicerca.setOnClickListener(new OnClickListener() {
public void onClick (View v){
txtNumeroCommessa.setText("");
txtSottoCommessa.setText("");
jsonlist.clear();
ListAdapter adapter = new SimpleAdapter(
LeggiUbicazione.this,
jsonlist,
R.layout.activity_reucupera_ubicazione_row,
new String[]{Campo1, Campo2, Campo3},
new int[]{R.id.Campo1, R.id.Campo2, R.id.Campo3}
);
setListAdapter(adapter);
txtNumeroCommessa.requestFocus();
}
});
btnSkip.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
txtSottoCommessa.requestFocus();
txtSottoCommessa.setText("0");
txtNumeroCommessa.requestFocus();
}
});
txtSottoCommessa.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus) {
if (txtSottoCommessa.getText().toString().equals(""))
{
} else {
//do job here when Edittext lose focus
new ProgressTask(LeggiUbicazione.this).execute();
}
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.leggi_ubicazione, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public class ProgressTask extends AsyncTask<String, Void, Boolean> {
private ProgressDialog dialog;
public ProgressTask(ListActivity activity) {
context = activity;
dialog = new ProgressDialog(context);
}
private Context context;
protected void onPreExecute() {
this.dialog.setMessage("Progress start");
this.dialog.show();
}
@Override
protected void onPostExecute(final Boolean success) {
if (dialog.isShowing()) {
dialog.dismiss();
}
ListAdapter adapter = new SimpleAdapter(
context,
jsonlist,
R.layout.activity_reucupera_ubicazione_row,
new String[]{Campo1, Campo2, Campo3},
new int[]{R.id.Campo1, R.id.Campo2, R.id.Campo3}
);
setListAdapter(adapter);
}
}
protected Boolean doInBackground(final String... args) {
jsonlist.clear();
JSONParser sitoLogin = new JSONParser();
String[] params = new String[2];
params[0] = txtNumeroCommessa.getText().toString();
params[1] = txtSottoCommessa.getText().toString();
JSONArray json;
try {
json = sitoLogin.getJSONFromUrl(url, params);
for (int i = 0; i < json.length(); i++) {
try {
JSONObject c = json.getJSONObject(i);
String campo1 = c.getString(Campo1);
String campo2 = c.getString(Campo2);
String campo3 = c.getString(Campo3);
HashMap<String, String> map = new HashMap<String, String>();
map.put(Campo1, campo1);
map.put(Campo2, campo2);
map.put(Campo3, campo3);
jsonlist.add(map);
}
catch (JSONException e) {
e.printStackTrace();
}
}
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return null;
}
}
}
你可以帮帮我吗?感谢。