2012-08-01 71 views
1

我有一個私有的Web服務,我需要通過身份驗證和JSONObject請求。 身份驗證進行得很完美,響應也可以,但作爲響應,我正在獲取特定Web服務實現頁面的HTML頁面源代碼。獲取HTML頁面源代碼而不是JSON響應

他們的任何問題與我發送的JSONObject請求。

請參考下面的代碼:

公衆的JSONObject getJSONFromUrl(字符串URL){ 嘗試 { DefaultHttpClient的HttpClient =新DefaultHttpClient();

 HttpPost httppost=new HttpPost(url); 
     httppost.setHeader("Authorization", "Basic "+Base64.encodeToString("abc:xyz".getBytes(), Base64.NO_WRAP)); 

     //Posting request on htt 
      httppost.setHeader("Content-Type", "application/json");   
      httppost.setHeader("Accept","application/json"); 
      JSONObject jsonPara = new JSONObject();  
      jsonPara.put("password", "abc"); 
      jsonPara.put("username", "abc"); 




      JSONObject jsonobj=new JSONObject(); 

      jsonobj.put("request", "syncdata/get_country"); 
      jsonobj.put("para",jsonPara); 

      Log.i("jason Object", jsonobj.toString()); 

      StringEntity se2 = new StringEntity(jsonobj.toString()); 

      se2.setContentEncoding("UTF-8"); 
      se2.setContentType("application/json"); 

      httppost.setEntity(se2); 
     HttpResponse httpResponse=httpClient.execute(httppost); 
     HttpEntity httpEntity=httpResponse.getEntity(); 
     is=httpEntity.getContent(); 
    } 
    catch(UnsupportedEncodingException ae) 
    { 
    ae.printStackTrace();   
    } 
    catch(ClientProtocolException ae) 
    { 
     ae.printStackTrace(); 
    } 
    catch(IOException ae) 
    { 
     ae.printStackTrace(); 
    } 
    catch(JSONException e) 
    { 
     e.printStackTrace(); 
    } 

    try 
    { 
     BufferedReader reader=new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); 
     StringBuilder sb=new StringBuilder(); 
     String line=null; 
     while((line= reader.readLine()) != null) 
     { 
      sb.append(line + "\n"); 
     } 
     is.close(); 
     json =sb.toString(); 
    } 
    catch(Exception ae) 
    { 
     ae.printStackTrace(); 
    } 

    try 
    { 
     jObj=new JSONObject(json); 
    } 
    catch(JSONException e) 
    { 
    e.printStackTrace();  
    } 

    return jObj; 
} 

而且我怎樣才能確認,我得到的響應是JSON或其他某種格式(其他然後調試)和我會得到我真正應該得到正確的響應。

回答

1

請參閱此代碼。

我希望它會幫助你...

MainActivity.java

public class MainActivity extends Activity { 

InputStream is=null; 
String result=null; 
String line=null; 

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

try 
{ 
HttpClient httpclient = new DefaultHttpClient(); 
HttpPost httppost = new HttpPost("http://10.0.2.2/text.php"); 
HttpResponse response = httpclient.execute(httppost); 
HttpEntity entity = response.getEntity(); 
is = entity.getContent(); 
Log.e("Pass 1", "connection success "); 
} 
catch(Exception e) 
{ 
Log.e("Fail 1", e.toString()); 
Toast.makeText(getApplicationContext(), "Invalid IP Address",Toast.LENGTH_LONG).show(); 
}  

try 
{ 
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); 
StringBuilder sb = new StringBuilder(); 
while ((line = reader.readLine()) != null) 
{ 
sb.append(line + "\n"); 
} 
is.close(); 
result = sb.toString(); 
Log.e("Pass 2", "connection success "); 
} 
catch(Exception e) 
{ 
Log.e("Fail 2", e.toString()); 
}  

try 
{ 
JSONArray JA=new JSONArray(result); 
JSONObject json= null; 
final String[] str1 = new String[JA.length()];  
for(int i=0;i<JA.length();i++) 
{ 
json=JA.getJSONObject(i); 
str1[i]=json.getString("name"); 
} 

final AutoCompleteTextView text = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1); 
final List<String> list = new ArrayList<String>(); 

for(int i=0;i<str1.length;i++) 
{ 
list.add(str1[i]); 
} 

Collections.sort(list); 

ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getApplicationContext(), 
       android.R.layout.simple_spinner_item, list); 
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
text.setThreshold(1); 
text.setAdapter(dataAdapter); 

text.setOnItemClickListener(new OnItemClickListener() { 
@Override 
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) { 
// TODO Auto-generated method stub 
Toast.makeText(getBaseContext(), list.get(arg2).toString(), Toast.LENGTH_SHORT).show(); 
} 
}); 

} 
catch(Exception e) 
{ 
Log.e("Fail 3", e.toString()); 
} 
} 

text.php

<?php 
$host='127.0.0.1'; 
$uname='root'; 
$pwd='password'; 
$db='android'; 
$con = mysql_connect($host,$uname,$pwd) or die("connection failed"); 
mysql_select_db($db,$con) or die("db selection failed"); 
$r=mysql_query("select * from class",$con); 
while($row=mysql_fetch_array($r)) 
{ 
$cls[]=$row; 
} 
print(json_encode($cls)); 
mysql_close($con); 
?> 
相關問題