【发布时间】:2014-05-26 06:44:48
【问题描述】:
我正在为 iOS 编写一个字典应用程序。
我需要从一种语言翻译成另一种语言。
我有 2 个文本视图。在第一个(textView1)中,我必须输入一个要翻译的单词,在第二个(textView)中,当我按下“翻译”按钮时,翻译必须出现。 所以我需要发送
{"SourceText":"%@","CheckCode":"something","TranslateDirection":"0","SubjectBase":"8"},[[self textView1] text].
假设我的 URL API 是:http://fakesite.com/api/fake。我做错了什么?
-(IBAction)translate
{
NSString *post = [NSString stringWithFormat:@"{\"SourceText\":\"%@\",\"CheckCode\":\something\",\"TranslateDirection\":\"0\",\"SubjectBase\":\"8\"}",[[self textView1] text]];
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://fakesite.com/api/fake"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded;charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSURLResponse *response;
NSData *POSTReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
NSString *theReply = [[NSString alloc] initWithBytes:[POSTReply bytes] length:[POSTReply length] encoding: NSUTF8StringEncoding ];
_textView.text=theReply;
}
当我按下“翻译按钮”时,第二个 textView 中会出现以下消息:{"Message":"An error has occurred."}
看,我已经在 Android 的 java 中完成了这个,但是由于我是 Xcode 的新手,所以我很困惑如何去做。这是我的 java 文件作为示例。
Mainactivity.java
public class MainActivity extends Activity implements OnClickListener {
public String URL = "http://fakesite.com/api/fake";
EditText text_input;
EditText output;
Button but_tr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.user_interface);
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
output = (EditText) findViewById(R.id.text_output);
but_tr = (Button) findViewById(R.id.button_translate);
// add click listener to Button "POST"
but_tr.setOnClickListener(this);
}
@Override
public void onClick(View view) {
String txt = text_input.getText().toString();
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
nameValuePairs.add(new BasicNameValuePair("SourceText", txt));
nameValuePairs.add(new BasicNameValuePair("CheckCode", "1q2w3e"));
nameValuePairs.add(new BasicNameValuePair("TranslateDirection", "0"));
nameValuePairs.add(new BasicNameValuePair("SubjectsBase", "8"));
API_Post post = new API_Post(URL, nameValuePairs);
String Response = post.postData().toString().replace("\\r\\n", "");
output.setText(Response.substring(51, Response.length() -2).replace("\\n", System.getProperty("line.separator")));
}
}
API_Post.java
public class API_Post {
String url;
List<NameValuePair> nameValuePairs;
public API_Post(String str, List<NameValuePair> params) {
this.url = str;
this.nameValuePairs = params;
}
public String postData() {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(this.url);
StringBuilder builder = new StringBuilder();
try {
httppost.setEntity(new UrlEncodedFormEntity(this.nameValuePairs, HTTP.UTF_8 ));
HttpResponse response = httpclient.execute(httppost);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
Log.d("RestClient", "Status Code : " + statusCode);
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(
content));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
{
builder.append(line);
}
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
return builder.toString();
}
}
所以我需要类似的 Xcode 代码。谢谢
【问题讨论】:
-
可以发原始网址吗?
-
是的。我可以使用 Postman chrome 客户端检查它
标签: android iphone objective-c json ios7