2011-09-23 101 views
0

我有一些代碼將用於使用其用戶名和密碼登錄到客戶端server。這是我正在使用的代碼。我的問題是它沒有正確認證。我檢查了URL,用戶名密碼&但仍有似乎有些八九不離十錯誤使用JSON對象登錄到服務器

public class HttpClient { 

private static final String TAG = "&&----HTTPClient-----**"; 
public static void SendHttpPost (String URL, JSONObject jsonObjSend){ 

    try{  
      DefaultHttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httpPostRequest = new HttpPost(URL); 
      Log.v("The url is","The url is "+URL); 

      StringEntity se; 
      se = new StringEntity(jsonObjSend.toString()); 

      httpPostRequest.setEntity(se); 
      httpPostRequest.setHeader("Accept", "application/json"); 
      httpPostRequest.setHeader("Content-type", "application/json"); 

      long t = System.currentTimeMillis(); 
      HttpResponse response = (HttpResponse) httpclient.execute(httpPostRequest); 
      Log.i(TAG, "HTTPRESPONSE RECIEVED" +(System.currentTimeMillis()-t) + "ms"); 
      HttpEntity entity = response.getEntity(); 

      if(entity != null){ 
       InputStream instream = entity.getContent(); 
       String resultString = convertStreamToString(instream); 
       Log.v(TAG , "The response is " +resultString); 
       instream.close(); 
       JSONObject jsonObj = new JSONObject(resultString); 
       JSONObject sessionJson = jsonObj.getJSONObject("session"); 
       String sessionId = sessionJson.getString("sessionid"); 
       String name = sessionJson.getString("name"); 
       Log.v("The name is"+name,""+sessionId); 
      } 
    } catch (Exception e){ 
     e.printStackTrace(); 
    } 
} 
private static String convertStreamToString(InputStream is) { 

    BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
    StringBuilder sb = new StringBuilder(); 

    String line = null; 
    try{ 
     while((line = reader.readLine()) !=null){ 
      sb.append(line + "\n"); 
     } 
    } 
     catch (IOException e){ 
      e.printStackTrace(); 
     } finally{ 
      try { 
       is.close(); 
      } catch (IOException e){ 
       e.printStackTrace(); 
      } 
     } 
     return sb.toString(); 

}}

這是HOMEACTIVITY

public class HomeActivity extends Activity { 

    private static final String tag = "##-----HomeActivity-----&&"; 
    private static final String URL = "**************************"; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     JSONObject jsonObjSend = new JSONObject(); 

     try { 
       JSONObject header = new JSONObject(); 
       header.put("username","125"); 
       header.put("password","1"); 
       header.put("company", "1000"); 
       jsonObjSend.put("user", header); 

       // Output the JSON object we're sending to Logcat: 
       Log.i(tag,"Output the JSON object we're sending to Logcat: " +jsonObjSend.toString(2)); 

       } catch (JSONException e) { 
       e.printStackTrace(); 
       } 


    } 
} 

在回家的網址活動我正在傳遞實際的服務器URL(不能共享)。

回答

1

你是不是調用homeactivity的httppost

試試這個

HttpClient.SendHttpPost(URL, jsonObjSend);