有兩個步驟是一個挑戰。 先做這個網址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
如果需要更多的信息,我很樂意提供
你的URL看起來像試圖使用參數,但它沒有'''分隔符 – Krease
好點,我意識到它,所以我添加了它,但我仍然得到相同的錯誤。我應該得到202作爲正確的答案 –
那麼,404基本上是沒有找到該網址 - 這就是我將重點放在你的調查試圖解決它。也許還有另一個錯字,或者別的什麼。 – Krease