How to fill dictionary from wcf ksoap2 response - c#

I have a wcf ksoap2 service that returns Dictionary<ArrayList, List<byte[]>>. Now at android side I want to fill my Dictionary<String[], ArrayList<Object>> diction; from wcf response. I am new to wcf and android/java, I don't have idea how to do this. Please provide me some better example of filling Dictionary with wcf.
Thanks in advance
This is my wcf code
public Dictionary<ArrayList, List<byte[]>> getImages()
{
Dictionary<ArrayList, List<byte[]>> image_Name = new Dictionary<ArrayList, List<byte[]>>();
DirectoryInfo directoryInfo = new DirectoryInfo(#"C:\Users\Yakhtar\Desktop\abc");
arr1 = new ArrayList();
foreach (FileInfo fi in directoryInfo.GetFiles())
arr1.Add(fi.FullName);
list = new List<byte[]>();
for (int i = 0; i < arr1.Count; i++)
{
img = Image.FromFile(arr1[i].ToString());
ms = new MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
list.Add(ms.ToArray());
}
image_Name.Add(arr1, list);
//image_Name[arr1 as ArrayList] = [list as byte[]];
return image_Name;
}

Well I am not sure about that but have you thought about JSON parsing instead of ksoap2 ??
Here is a tutorial on how to work with array of complex objects with KSOAP. I found out by countless hours of debugging. Hope this hepls
also try this
SoapObject countryDetails = (SoapObject)envelope.getResponse();
System.out.println(countryDetails.toString());
ArrayList list = new ArrayList(countryDetails.getPropertyCount());
lv_arr = new String[countryDetails.getPropertyCount()];
for (int i = 0; i < countryDetails.getPropertyCount(); i++) {
Object property = countryDetails.getProperty(i);
if (property instanceof SoapObject) {
SoapObject countryObj = (SoapObject) property;
String countryName = countryObj.getProperty("countryName").toString();
list.add(countryName );
}
}

Do something like this..
list = new List<byte[]>();
for (int i = 0; i < arr1.Count; i++)
{
img = Image.FromFile(arr1[i].ToString());
ms = new MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
list.Add(ms.ToArray());
}
image_Name.Add(arr1, list);
//image_Name[arr1 as ArrayList] = [list as byte[]];
return image_Name;
}

Related

Concatenating lists horizontally C#

I'm using the CsvHelper library tool to help write lists that I've created to CSV file.
using (var sr = new StreamReader(inPath))
{
using (var sw = new StreamWriter(outPath))
{
var reader = new CsvReader(sr);
var writer = new CsvWriter(sw);
IEnumerable records = reader.GetRecords<DataRecord>().ToList();
List<CountAndFrequencyClass> list1 = new List<CountAndFrequencyClass>();
list1 = CountAndFrequency(records, "ShipperName", 1);
List<CountAndFrequencyClass> list2 = new List<CountAndFrequencyClass>();
list2 = CountAndFrequency(records, "ShipperCity", 1);
list1 = list1.Concat(list2).ToList();
writer.WriteRecords(list2);
}
}
The list1=list1.Concat(list2).ToList(); does indeed concatenate the strings, but it stacks them on top of each other when they're written out to the CSV file. I want to find a way to concatenate the lists horizontally (so they're displayed next to eachother) instead of vertically.
Thanks for any help and please let me know if additional information is needed!
You can use a loop to add list2 column to records of list1
foreach (var i=0 ; i< list1.Count; i++)
{
if(i>=list2.Count ) break;
var rec1 = list1[i];
var rec2 = list2[i];
rec1.NewColumn = rec2.ColumnToAdd;
}
You could write them to the file as you loop through the lists.
using (var sr = new StreamReader(inPath))
{
using (var sw = new StreamWriter(outPath))
{
var reader = new CsvReader(sr);
var writer = new CsvWriter(sw);
IEnumerable records = reader.GetRecords<DataRecord>().ToList();
List<CountAndFrequencyClass> list1 = new List<CountAndFrequencyClass>();
list1 = CountAndFrequency(records, "ShipperName", 1);
List<CountAndFrequencyClass> list2 = new List<CountAndFrequencyClass>();
list2 = CountAndFrequency(records, "ShipperCity", 1);
for (int i = 0; i < list1.Count; i++)
{
writer.WriteRecord(list1[i]);
writer.WriteRecord(list2[i]);
writer.NextRecord();
}
}
}

NPOI converting XLS to XLSX throwing null reference

I'm trying to convert a file from XLS to XLSX using NPOI. As I'm not aware of an explicit conversion, I wrote this first implementation going through the rows and cells and copying from one to another:
public string ConvertToXlsx(string xlsPath)
{
var oldWorkbook = new HSSFWorkbook(new FileStream(xlsPath, FileMode.Open));
var oldWorkSheet = oldWorkbook.GetSheetAt(0);
var newExcelPath = xlsPath.Replace("xls", "xlsx");
using (var fileStream = new FileStream(newExcelPath, FileMode.Create))
{
var newWorkBook = new XSSFWorkbook();
var newWorkSheet = new XSSFSheet();
newWorkBook.Add(newWorkSheet);
foreach (HSSFRow oldRow in oldWorkSheet)
{
var newRow = newWorkSheet.CreateRow(oldRow.RowNum);
for (int ii = oldRow.FirstCellNum; ii < oldRow.LastCellNum; ii++)
{
var newCell = newRow.CreateCell(ii);
newCell = oldRow.Cells[ii];
}
}
newWorkBook.Write(fileStream);
}
return newExcelPath;
}
Yet, on line var newCell = newRow.CreateCell(ii); NPOI throws a NullReferenceException With the following stack trace:
at NPOI.XSSF.UserModel.XSSFCell..ctor(XSSFRow row, CT_Cell cell)
at NPOI.XSSF.UserModel.XSSFRow.CreateCell(Int32 columnIndex, CellType type)
at NPOI.XSSF.UserModel.XSSFRow.CreateCell(Int32 columnIndex)
at Ing2Ynab.Excel.IngExcelConverter.ConvertToXlsx(String xlsPath)
Which I don't get why it's happening, as XSSFRow should be in charge of creating the CT_Cell that gets passed on to XSSFCell constructor, from what I could read in NPOIs code.
Has anyone else tried to do this and/or has fixed it?
Thanks.
Looks like you have to explicitly call the Workbooks CreateSheet() method instead of calling .Add(). Additionally, you seem to have some out of range exceptions on your loop so keep an eye out for that.
public string ConvertToXlsx(string xlsPath)
{
var oldWorkbook = new HSSFWorkbook(new FileStream(xlsPath, FileMode.Open));
var oldWorkSheet = oldWorkbook.GetSheetAt(0);
var newExcelPath = xlsPath.Replace("xls", "xlsx");
using (var fileStream = new FileStream(newExcelPath, FileMode.Create))
{
var newWorkBook = new XSSFWorkbook();
var newWorkSheet = newWorkBook.CreateSheet("Sheet1");
foreach (HSSFRow oldRow in oldWorkSheet)
{
var newRow = newWorkSheet.CreateRow(oldRow.RowNum);
for (int ii = oldRow.FirstCellNum; ii < oldRow.LastCellNum; ii++)
{
var newCell = newRow.CreateCell(ii);
newCell = oldRow.Cells[ii];
}
}
newWorkBook.Write(fileStream);
}
return newExcelPath;
}

Returning a list back to the function

This is my code. Im using the function to retrieve the list but its not sending it.
public List<string> country_set()
{
mCountryUrl = new Uri ("http://xxxxxxx.wwww/restservice/country");
mList = new List<string> ();
mCountry = new List<Country> ();
WebClient client = new WebClient ();
client.DownloadDataAsync (mCountryUrl);
client.DownloadDataCompleted += (sender, e) => {
RunOnUiThread (() => {
string json = Encoding.UTF8.GetString (e.Result);
mCountry = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Country>> (json);
Console.WriteLine (mCountry.Count.ToString());
int x = mCountry.Count;
for(int i=0; i< x ; i++)
{
mList.Add(mCountry[i].name);
}
});
};
return mList;
}
It throws an exception .
Kindly help me out
The problem is that you return the mList immediately after your method completes which is before the call to the web server completes. Now after your calling code inspects the list to find it empty, eventually the call to the server will complete and your list will be filled which is too late!
This will fix the problem:
var mCountryUrl = new Uri("http://xxxxxxx.wwww/restservice/country");
var mList = new List<string>();
var mCountry = new List<Country>();
WebClient client = new WebClient();
var data = client.DownloadData(mCountryUrl);
string json = Encoding.UTF8.GetString(data);
mCountry = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Country>>(json);
Console.WriteLine(mCountry.Count.ToString());
int x = mCountry.Count;
for (int i = 0; i < x; i++)
{
mList.Add(mCountry[i].name);
}
return mList;
How about this one:
public async Task<List<string>> country_set()
{
mCountryUrl = new Uri ("http://xxxxxxx.wwww/restservice/country");
mList = new List<string>();
mCountry = new List<Country>();
WebClient client = new WebClient();
byte[] data = await client.DownloadDataTaskAsync(mCountryUrl);
string json = Encoding.UTF8.GetString(data);
mCountry = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Country>> (json);
Console.WriteLine (mCountry.Count.ToString());
int x = mCountry.Count;
for(int i=0; i<x; i++)
mList.Add(mCountry[i].name);
return mList;
}
It uses the new async model from .Net.
EDIT: Code is typed from the Android app. Anyone who spots syntax mistakes (or any other kind), please signal them in a comment.

How to combine lists into single data entity in c#

I have a web method in a c# web service which creates three lists, which are filled from xml input. I want to combine these three lists into one entity (a DataSet would be the best, as the iOS app that is consuming this web service is already programmed to accept and parse DataSets), and return them from the web method.
Here is currently what my code looks like:
[WebMethod]
public DataSet SelectObjects(string ExternalID, string Password)
{
DataSet ds = new DataSet();
MembershipAuthServiceReference.MembershipAuthenticationService objService = new MembershipAuthServiceReference.MembershipAuthenticationService();
MembershipAuthServiceReference.SoapHeaderCredentials objSoapHeader = new MembershipAuthServiceReference.SoapHeaderCredentials();
MembershipAuthServiceReference.MemberUserInfo objMemberInfo = new MembershipAuthServiceReference.MemberUserInfo();
try
{
objSoapHeader.UserName = ExternalID;
objSoapHeader.Password = Password;
objMemberInfo = objService.GetMembershipInfo();
List<Obj1> ListObj1 = new List<Obj1>();
for (int i = 0; i < objMemberInfo.Obj1.Length; i++)
{
Obj1 obj_Obj1 = new Obj1();
obj_Obj1.Stuff = objMemberInfo.Obj1[i].Stuff.ToString();
ListObj1.Add(obj_Obj1);
}
List<Obj2> ListObj2 = new List<Obj2>();
for (int i = 0; i < objMemberInfo.Obj2.Length; i++)
{
Obj2 obj_Obj2 = new Obj2();
obj_Obj2.Stuff = objMemberInfo.Obj2[i].Stuff.ToString();
ListObj2.Add(obj_Obj2);
}
List<Obj3> ListObj3 = new List<Obj3>();
for (int i = 0; i < objMemberInfo.Obj3.Length; i++)
{
Obj3 obj_Obj3 = new Obj3();
obj_Obj3.Stuff = objMemberInfo.Obj3[i].Stuff.ToString();
ListObj3.Add(obj_Obj3);
}
}
catch (Exception ex)
{
string sError;
sError = ex.Message.ToString();
}
return ds;
}
How do I combine these lists into a DataSet? I'm assuming it's possible? If not, is there a viable alternative that does the same thing?
First concatenate your lists as shown below and then use the link to generate the dataset
var combinedList= ListObj1.Concat(ListObj2).Concat(ListObj3);
How do I transform a List<T> into a DataSet?

can someone help me with a proxy list

Okay someone said to put my proxies in a list so I can use a new proxy for each request, I have the proxies saved to a list but I am a little confused on where to go from here
This is the code that I used to generate the proxie list, and it's coming from a proxies.txt file.
private List<string> getProxyListFromText(string input) {
List<string> list = new List<string>();
StreamReader reader = new StreamReader(input);
string item = "";
while (item != null)
{
item = reader.ReadLine();
if (item != null)
{
list.Add(item);
}
}
reader.Close();
return list;
}
ok here is the request, each request should retreive a different proxie from the list.
Picture a for loop that is looping through a list of names, each name brings up a different request, and each request should have it's own proxy, the proxy list is already generated in the code above just need a way i can retreive proxies from the list.
for (int i = 0; i < listBox1.Items.Count; i++)
{
object url;
WebClient wc;
url = getppl;
wc = new WebClient();
//This should come from the proxy list
wc.Proxy = new WebProxy(getProxyListFromText("Proxies.txt"));
var page = wc.DownloadString(url.ToString());
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(page);
var pplname = doc.DocumentNode.SelectNodes("/html/body/div[3]/div/div[2]/div[2]/div/div[4]/p");
}
I tried a nested for loop but the logic got tied up somewhere.
Change to:-
foreach(string prox in getProxyListFromText("Proxies.txt"))
{
...
wc.Proxy = new WebProxy(prox);
...
}

Categories