2013-11-28 93 views
-1

有兩個步驟是一個挑戰。 先做這個網址http post使用java中的json錯誤

http://mkpartners.force.com/services/apexrest/careers 

通過增加3字段名,姓氏獲取和電子郵件

http://mkpartners.force.com/services/apexrest/careers?firstName=Izak&lastName=Tarashandegan&[email protected] 

我做的那部分 和第二部分是在這個網址,我構建的帖子和過去在我的簡歷作爲json文件到該網址,但我得到400 HTTP錯誤,我不明白爲什麼?

這是我

package posthttp; 
import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.net.MalformedURLException; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.entity.StringEntity; 
import org.apache.http.impl.client.DefaultHttpClient; 
/** 
* 
* @author Izak 
*/ 
public class PostHTTP { 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     try { 

     DefaultHttpClient httpClient = new DefaultHttpClient(); 
     HttpPost postRequest = new HttpPost(
      "http://mkpartners.force.com/services/apexrest/careers?firstName=Izak&lastName=Tarashandegan&[email protected]"); 
       String json = "{\"application\":{\"isTest\":true,\"firstName\":\"Izak\",\"lastName\":\"Tarashandegan\",\"email\":\"[email protected]\",\"phone\":\"4244420591\",\"zipcode\":\"90035\",\"describeYourself\":\"I am passionate about programming.I do my best to learn new materials about programming everyday. My favorite language is Java, and I do all my coding in Java.\",\"skills\":\"I can talk in English and Farsi fluently, and I know a little Spanish too\",\"education\":[{\"school\":\"CSUN\",\"graduationYear\":2013,\"degree\":\"bachelor\",\"major\":\"CS\"},{\"school\":\"SMC\",\"graduationYear\":2010,\"degree\":\"AA\",\"major\":\"CS\"}],\"experience\":[{\"company\":\"Ambient Digital Media\",\"fromDate\":\"2004-02-01\",\"toDate\":\"2004-05-01\",\"title\":\"web desinger\",\"workDone\":\"update their website\"},{\"company\":\"liberty plumbing and heating Inc.\",\"fromDate\":\"2004-06-01\",\"toDate\":\"2013-05-15\",\"title\":\"web desinger\",\"workDone\":\"work as a book keeper\"}]}"; 
     StringEntity input = new StringEntity(json); 
     input.setContentType("application/json"); 
     postRequest.setEntity(input); 

     HttpResponse response = httpClient.execute(postRequest); 

     if (response.getStatusLine().getStatusCode() != 201) { 
      throw new RuntimeException("Failed : HTTP error code : " 
       + response.getStatusLine().getStatusCode()); 
     } 

     BufferedReader br = new BufferedReader(
         new InputStreamReader((response.getEntity().getContent()))); 

     String output; 
     System.out.println("Output from Server .... \n"); 
     while ((output = br.readLine()) != null) { 
      System.out.println(output); 
     } 

     httpClient.getConnectionManager().shutdown(); 

     } catch (MalformedURLException e) { 

     e.printStackTrace(); 

     } catch (IOException e) { 

     e.printStackTrace(); 

     } 
    } 

} 

我已經是它給我的錯誤HTTP錯誤代碼400的問題代碼,但我跟着一切,據我所知。 確切的錯誤是

Exception in thread "main" java.lang.RuntimeException: Failed : HTTP error code : 400 
at posthttp.PostHTTP.main(PostHTTP.java:41) 

Java結果:1

如果需要更多的信息,我很樂意提供

+1

你的URL看起來像試圖使用參數,但它沒有'''分隔符 – Krease

+0

好點,我意識到它,所以我添加了它,但我仍然得到相同的錯誤。我應該得到202作爲正確的答案 –

+0

那麼,404基本上是沒有找到該網址 - 這就是我將重點放在你的調查試圖解決它。也許還有另一個錯字,或者別的什麼。 – Krease

回答

1

你缺少最後收}。此外,當我做了POST,我得到了迴應:

[{"errorCode":"JSON_PARSER_ERROR","message":"Expected LIST<String> but found \" I can talk in English and Farsi fluently, and I know a little Spanish too\" 

所以,你需要發送JSON列表,而不是一個單一的條目。

String json = "http://mkpartners.force.com/services/apexrest/careers?firstName=Izak&lastName=Tarashandegan&[email protected]"; 
data = "{\"application\":{\"isTest\":true,\"firstName\":\"Izak\",\"lastName\":\"Tarashandegan\",\"email\":\"[email protected]\",\"phone\":\"4244420591\",\"zipcode\":\"90035\",\"describeYourself\":\"I am passionate about programming.I do my best to learn new materials about programming everyday. My favorite language is Java, and I do all my coding in Java.\",\"skills\":[\"I can talk in English and Farsi fluently, and I know a little Spanish too\"],\"education\":[{\"school\":\"CSUN\",\"graduationYear\":2013,\"degree\":\"bachelor\",\"major\":\"CS\"},{\"school\":\"SMC\",\"graduationYear\":2010,\"degree\":\"AA\",\"major\":\"CS\"}],\"experience\":[{\"company\":\"Ambient Digital Media\",\"fromDate\":\"2004-02-01\",\"toDate\":\"2004-05-01\",\"title\":\"web desinger\",\"workDone\":\"update their website\"},{\"company\":\"liberty plumbing and heating Inc.\",\"fromDate\":\"2004-06-01\",\"toDate\":\"2013-05-15\",\"title\":\"web desinger\",\"workDone\":\"work as a book keeper\"}]}}"; 

新迴應:

"Your TEST POST is correct, please set the isTest (Boolean) attribute on the application to FALSE to actually apply.

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.net.MalformedURLException; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.entity.StringEntity; 
import org.apache.http.impl.client.DefaultHttpClient; 
/** 
* 
* @author Izak 
*/ 
public class PostHTTP { 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     try { 

     DefaultHttpClient httpClient = new DefaultHttpClient(); 
     HttpPost postRequest = new HttpPost(
      "http://mkpartners.force.com/services/apexrest/careers?firstName=Izak&lastName=Tarashandegan&[email protected]"); 
       String json = "{\"application\":{\"isTest\":true,\"firstName\":\"Izak\",\"lastName\":\"Tarashandegan\",\"email\":\"[email protected]\",\"phone\":\"4244420591\",\"zipcode\":\"90035\",\"describeYourself\":\"I am passionate about programming.I do my best to learn new materials about programming everyday. My favorite language is Java, and I do all my coding in Java.\",\"skills\":[\"I can talk in English and Farsi fluently, and I know a little Spanish too\"],\"education\":[{\"school\":\"CSUN\",\"graduationYear\":2013,\"degree\":\"bachelor\",\"major\":\"CS\"},{\"school\":\"SMC\",\"graduationYear\":2010,\"degree\":\"AA\",\"major\":\"CS\"}],\"experience\":[{\"company\":\"Ambient Digital Media\",\"fromDate\":\"2004-02-01\",\"toDate\":\"2004-05-01\",\"title\":\"web desinger\",\"workDone\":\"update their website\"},{\"company\":\"liberty plumbing and heating Inc.\",\"fromDate\":\"2004-06-01\",\"toDate\":\"2013-05-15\",\"title\":\"web desinger\",\"workDone\":\"work as a book keeper\"}]}}"; 
     StringEntity input = new StringEntity(json); 
     input.setContentType("application/json"); 
     postRequest.setEntity(input); 

     HttpResponse response = httpClient.execute(postRequest); 

     StringBuilder sb = new StringBuilder(); 
     try { 
      BufferedReader reader = 
        new BufferedReader(new InputStreamReader(response.getEntity().getContent()), 65728); 
      String line = null; 

      while ((line = reader.readLine()) != null) { 
       sb.append(line); 
      } 
     } 
     catch (IOException e) { e.printStackTrace(); } 
     catch (Exception e) { e.printStackTrace(); } 


System.out.println("finalResult " + sb.toString()); 
     System.out.println(response.getStatusLine().getReasonPhrase()); 

     if (response.getStatusLine().getStatusCode() != 201) { 
      throw new RuntimeException("Failed : HTTP error code : " 
       + response.getStatusLine().getStatusCode()); 
     } 

     BufferedReader br = new BufferedReader(
         new InputStreamReader((response.getEntity().getContent()))); 

     String output; 
     System.out.println("Output from Server .... \n"); 
     while ((output = br.readLine()) != null) { 
      System.out.println(output); 
     } 

     httpClient.getConnectionManager().shutdown(); 

     } catch (MalformedURLException e) { 

     e.printStackTrace(); 

     } catch (IOException e) { 

     e.printStackTrace(); 

     } 
    } 

} 
+0

@Loskit,我添加了你所說的,仍然得到相同的結果,即400 HTTp錯誤。 我意識到另一個問題,要問這個數據變量在哪裏? –

+0

除了錯誤代碼之外,還要讀取API告訴你的錯誤消息嗎?打印出'response.getStatusLine()。getReasonPhrase()' –

+0

它沒有提供任何有用的信息,只有400個http錯誤代碼。我得到的是 異常在線程「main」java.lang.RuntimeException:失敗:HTTP錯誤代碼:400 \t at posthttp.PostHTTP.main(PostHTTP.java:41) Java結果:1 –