将用java编写的HttpClient转换为C#以调用Web API
我对C#和windows应用程序编程非常陌生 我正在尝试创建一个异步任务,就像在java中一样,在这里我可以查询url并获取其响应 这是我通常在java中使用的代码,我想用C语言实现拷贝将用java编写的HttpClient转换为C#以调用Web API,java,c#,Java,C#,我对C#和windows应用程序编程非常陌生 我正在尝试创建一个异步任务,就像在java中一样,在这里我可以查询url并获取其响应 这是我通常在java中使用的代码,我想用C语言实现拷贝 public interface ResponseCallback { void onSuccess(String response); void onFailure(String exception); } public class MyAsyncTask extends AsyncTas
public interface ResponseCallback
{
void onSuccess(String response);
void onFailure(String exception);
}
public class MyAsyncTask extends AsyncTask<String, Void, String>
{
private ResponseCallback myResponse = null;
private int type = 0;//POST
List<NameValuePair> nameValuePairs = null;
StringEntity entity = null;
private HttpResponse response = null;
public MyAsyncTask(String url,ResponseCallback myResponse)
{
this.myResponse = myResponse;
this.execute(url);
}
@Override
protected String doInBackground(String... param)
{
String url = param[0];
response = null;
HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter("http.connection-manager.timeout", 15000);
try {
if (type == 0)
{
HttpPost httppost = new HttpPost(url);
if (nameValuePairs != null)
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
if (entity != null)
httppost.setEntity(entity);
response = httpclient.execute(httppost);
}
else
{
HttpGet httppost = new HttpGet(url);
response = httpclient.execute(httppost);
}
} catch (ClientProtocolException es)
{
} catch (IOException e)
{
}
String resp = null;
if (response != null)
{
try {
resp = Utilities.convertStreamToString(response.getEntity().getContent());
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return resp;
}
else
return null;
}
@Override
protected void onPostExecute(String resp)
{
if (resp != null)
{
if (response.getStatusLine().getStatusCode() == Constants.RESULT_OK )
{
try {
myResponse.onSuccess(resp.trim());
} catch (IllegalStateException e) {
}
}
else
myResponse.onFailure(resp);
}
else
myResponse.onFailure(resp);
}
}
您的
回调
字段是一个实例字段,因此您不能从静态方法访问它,除非您将该字段设置为静态
不过,我想推荐的另一种选择是根本不使用字段。将回调变量作为方法参数传递
或者你可以完全停止使用静态方法,让它们成为实例方法。我不完全确定它是否能满足你的所有需要,但我会考虑使用该类中可用的
…Async
函数来异步下载。@MichaelTodd有几点,我想检查,首先我不能调用callback.onSuccess(jsonStr);为什么我不能使用这个变量?第二,获取响应字符串的正确方法您是否设置了回调函数?Where?@MichaelTodd我在MyAsyncTask中定义了一个接口和一个变量,请检查我如何在Java中使用接口如果“回调变量未被识别”,则错误在于设置回调变量。我会首先考虑解决这个问题,然后担心您的HttpClient转换是否成功。
namespace The_Vow.Global
{
class MyAsyncTask
{
public ResponseCallback callback;
static void queryUrl(String url)
{
RunAsync(url).Wait();
}
static async Task RunAsync(String url)
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("MY_IP");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
// HTTP GET
HttpResponseMessage response = await client.GetAsync(url);
//response = await client.PostAsJsonAsync("api/products", gizmo);
if (response.IsSuccessStatusCode)
{
String jsonStr = await response.Content.ReadAsStringAsync();
// callback variable is not being recognized????
callback.onSuccess(jsonStr);
//Console.WriteLine("{0}\t${1}\t{2}", product.Name, product.Price, product.Category);
}
}
}
}
}
namespace The_Vow.Global
{
public interface ResponseCallback
{
void onSuccess(String response);
void onFailure(String exception);
}
}